<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Bitmasks mit defines machen Probleme]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich hab ein Problem welches ich momentan nicht nachvollziehen kann. Folgender Code macht nicht was er soll:</p>
<pre><code>if ((MY_BIT_MASK | state) == MY_BIT_MASK)
    std::cout &lt;&lt; &quot;valid!\n&quot;;
else
    std::cout &lt;&lt; &quot;invalid!\n&quot;;
</code></pre>
<p>Hier wird valid ausgegeben, obwohl MY_BIT_MASK = 56 und state = 64 ist. Wenn ich das Ganze explizit hinschreibe, also so:</p>
<pre><code>if ((56 | 64) == 56)
    std::cout &lt;&lt; &quot;valid!\n&quot;;
else
    std::cout &lt;&lt; &quot;invalid!\n&quot;;
</code></pre>
<p>dann funktioniert es auf einmal und es wird invalid ausgegeben - obwohl das doch genau das gleiche sein sollte wie der obige Code. Die Werte von MY_BIT_MASK und state hab ich mir schon ausgeben lassen und diese sind genau 56 und 64...</p>
<p>Was läuft hier schief?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/323771/bitmasks-mit-defines-machen-probleme</link><generator>RSS for Node</generator><lastBuildDate>Wed, 15 Jul 2026 21:12:31 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/323771.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 15 Feb 2014 14:03:57 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Bitmasks mit defines machen Probleme on Sat, 15 Feb 2014 14:03:57 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich hab ein Problem welches ich momentan nicht nachvollziehen kann. Folgender Code macht nicht was er soll:</p>
<pre><code>if ((MY_BIT_MASK | state) == MY_BIT_MASK)
    std::cout &lt;&lt; &quot;valid!\n&quot;;
else
    std::cout &lt;&lt; &quot;invalid!\n&quot;;
</code></pre>
<p>Hier wird valid ausgegeben, obwohl MY_BIT_MASK = 56 und state = 64 ist. Wenn ich das Ganze explizit hinschreibe, also so:</p>
<pre><code>if ((56 | 64) == 56)
    std::cout &lt;&lt; &quot;valid!\n&quot;;
else
    std::cout &lt;&lt; &quot;invalid!\n&quot;;
</code></pre>
<p>dann funktioniert es auf einmal und es wird invalid ausgegeben - obwohl das doch genau das gleiche sein sollte wie der obige Code. Die Werte von MY_BIT_MASK und state hab ich mir schon ausgeben lassen und diese sind genau 56 und 64...</p>
<p>Was läuft hier schief?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2383317</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2383317</guid><dc:creator><![CDATA[happystudent]]></dc:creator><pubDate>Sat, 15 Feb 2014 14:03:57 GMT</pubDate></item><item><title><![CDATA[Reply to Bitmasks mit defines machen Probleme on Sat, 15 Feb 2014 14:07:45 GMT]]></title><description><![CDATA[<p>Wie ist MY_BIT_MASK definiert?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2383318</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2383318</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sat, 15 Feb 2014 14:07:45 GMT</pubDate></item><item><title><![CDATA[Reply to Bitmasks mit defines machen Probleme on Sat, 15 Feb 2014 14:13:00 GMT]]></title><description><![CDATA[<p>Folgendermaßen:</p>
<pre><code>#define MASK_FLAG_4	0x08
#define MASK_FLAG_5	0x10
#define MASK_FLAG_6	0x20

#define MY_BIT_MASK	MASK_FLAG_4 | MASK_FLAG_5 | MASK_FLAG_6
</code></pre>
<p>Die Variable &quot;state&quot; hab ich zu testzwecken manuell auf 64 gesetzt, also folgendermaßen:</p>
<pre><code>int state = 0x40;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2383320</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2383320</guid><dc:creator><![CDATA[happystudent]]></dc:creator><pubDate>Sat, 15 Feb 2014 14:13:00 GMT</pubDate></item><item><title><![CDATA[Reply to Bitmasks mit defines machen Probleme on Sat, 15 Feb 2014 14:17:07 GMT]]></title><description><![CDATA[<p>Nachdem der Präprozessor durch ist, sieht das dann wie folgt aus:</p>
<pre><code>if ((0x08 | 0x10 | 0x20 | state) == 0x08 | 0x10 | 0x20)
</code></pre>
<p>Merkste was?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2383321</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2383321</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sat, 15 Feb 2014 14:17:07 GMT</pubDate></item><item><title><![CDATA[Reply to Bitmasks mit defines machen Probleme on Sat, 15 Feb 2014 14:17:00 GMT]]></title><description><![CDATA[<p>Da fehlen Klammern:</p>
<pre><code class="language-cpp">#define MY_BIT_MASK (MASK_FLAG_4 | MASK_FLAG_5 | MASK_FLAG_6)
</code></pre>
<p><a href="http://de.cppreference.com/w/cpp/language/operator_precedence" rel="nofollow">http://de.cppreference.com/w/cpp/language/operator_precedence</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2383322</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2383322</guid><dc:creator><![CDATA[Bashar]]></dc:creator><pubDate>Sat, 15 Feb 2014 14:17:00 GMT</pubDate></item><item><title><![CDATA[Reply to Bitmasks mit defines machen Probleme on Sat, 15 Feb 2014 14:20:52 GMT]]></title><description><![CDATA[<p>Oh, tatsächlich jetzt funktioniert es.</p>
<p>Aber ich dachte der kann das gleich auswerten? Also die flags sind ja allesamt Konstanten, kann man dem Präprozessor nicht sagen dass er das gleich zu einer fertigen Zahl evaluieren soll?</p>
<p>Also ich könnte es ja auch manuell hinschreiben, etwa so:</p>
<pre><code>#define MY_BIT_MASK 56
</code></pre>
<p>aber dann liest es sich halt nicht mehr so schön, da sich dann jeder fragen wird woher jetzt die 56 kommt...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2383323</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2383323</guid><dc:creator><![CDATA[happystudent]]></dc:creator><pubDate>Sat, 15 Feb 2014 14:20:52 GMT</pubDate></item><item><title><![CDATA[Reply to Bitmasks mit defines machen Probleme on Sat, 15 Feb 2014 14:24:45 GMT]]></title><description><![CDATA[<p>Der Präprozessor macht Textersetzung, mehr nicht.</p>
<p>edit: Außerdem, wenn er irgendwas auswerten würde, dann sicher nicht so, dass sich dadurch die Bedeutung des Programms verändert!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2383324</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2383324</guid><dc:creator><![CDATA[Bashar]]></dc:creator><pubDate>Sat, 15 Feb 2014 14:24:45 GMT</pubDate></item><item><title><![CDATA[Reply to Bitmasks mit defines machen Probleme on Sat, 15 Feb 2014 14:46:01 GMT]]></title><description><![CDATA[<p>Warum benutzt du Makros statt Konstanten?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2383325</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2383325</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Sat, 15 Feb 2014 14:46:01 GMT</pubDate></item><item><title><![CDATA[Reply to Bitmasks mit defines machen Probleme on Sat, 15 Feb 2014 14:49:28 GMT]]></title><description><![CDATA[<p>manni66 schrieb:</p>
<blockquote>
<p>Warum benutzt du Makros statt Konstanten?</p>
</blockquote>
<p>+1<br />
Wie gesagt: der Präprozessor macht lediglich Textersetzung.<br />
Du kannst auch schreiben:</p>
<pre><code>#define FOO dasistkeingültigerC++Programmcode,aber egal, der Präprozessor fügt das schön für mich einfach ein
</code></pre>
<p>Wenn du eine Konstante hast, wie:</p>
<pre><code>const /* constexpr wenn C++11 verfügbar */ int bit_mask = a | b | c;
</code></pre>
<p>Dann wird a | b | c ausgerechnet und in bit_mask gespeichert. Wenn a | b | c ein konstanter Ausdruck ist, wird im Endeffekt auch so etwas wie Textersetzung gemacht, nur korrekt. Da steht dann das Ergebnis von a | b | c, nicht die Operation selber.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2383326</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2383326</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sat, 15 Feb 2014 14:49:28 GMT</pubDate></item><item><title><![CDATA[Reply to Bitmasks mit defines machen Probleme on Sat, 15 Feb 2014 14:55:18 GMT]]></title><description><![CDATA[<p>Nathan schrieb:</p>
<blockquote>
<pre><code>const /* constexpr wenn C++11 verfügbar */ int bit_mask = a | b | c;
</code></pre>
</blockquote>
<p>Welchen Vorteil bringt da <code>constexpr</code> ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2383327</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2383327</guid><dc:creator><![CDATA[c++11fanatiker]]></dc:creator><pubDate>Sat, 15 Feb 2014 14:55:18 GMT</pubDate></item><item><title><![CDATA[Reply to Bitmasks mit defines machen Probleme on Sat, 15 Feb 2014 14:57:15 GMT]]></title><description><![CDATA[<p>Grund #1 wieso Makros böse sind. Man verwendet keine Makros, ganz besonders nicht für Konstanten...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2383328</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2383328</guid><dc:creator><![CDATA[dot]]></dc:creator><pubDate>Sat, 15 Feb 2014 14:57:15 GMT</pubDate></item><item><title><![CDATA[Reply to Bitmasks mit defines machen Probleme on Sat, 15 Feb 2014 15:14:03 GMT]]></title><description><![CDATA[<p>c++11fanatiker schrieb:</p>
<blockquote>
<p>Nathan schrieb:</p>
<blockquote>
<pre><code>const /* constexpr wenn C++11 verfügbar */ int bit_mask = a | b | c;
</code></pre>
</blockquote>
<p>Welchen Vorteil bringt da <code>constexpr</code> ?</p>
</blockquote>
<p>Keinen richtigen. Wenn das rechts der Zuweisung ein konstaner Ausdruck ist, ist bit_mask auch ein konstanter Ausdruck. Wenn allerdings aus <a href="http://ideone.com/HMK6CR" rel="nofollow">irgendwelchen Gründen</a> das rechts der Zuweisung kein konstanter Ausdruck ist, gibt es mit constexpr direkt einen Fehler.<br />
Außerdem dokumentiert das besser.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2383329</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2383329</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sat, 15 Feb 2014 15:14:03 GMT</pubDate></item><item><title><![CDATA[Reply to Bitmasks mit defines machen Probleme on Sat, 15 Feb 2014 16:21:19 GMT]]></title><description><![CDATA[<p>Nathan schrieb:</p>
<blockquote>
<p>Wenn allerdings aus <a href="http://ideone.com/HMK6CR" rel="nofollow">irgendwelchen Gründen</a> das rechts der Zuweisung kein konstanter Ausdruck ist, gibt es mit constexpr direkt einen Fehler.</p>
</blockquote>
<p>Überzeugt mich nicht. 1. Wieso sollte ich Konstanten erst später deklarieren? 2. Der Compiler kann das soweiso optimieren, falls die Konstante nicht in constexpr-Ausdrücken gebraucht wird, erhalte ich auch keine Fehlermeldung. Und falls doch, dann habe ich eine, aber die hätte ich mit constexpr auch.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2383343</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2383343</guid><dc:creator><![CDATA[c++11fanatiker]]></dc:creator><pubDate>Sat, 15 Feb 2014 16:21:19 GMT</pubDate></item><item><title><![CDATA[Reply to Bitmasks mit defines machen Probleme on Sat, 15 Feb 2014 16:27:33 GMT]]></title><description><![CDATA[<p>Nathan schrieb:</p>
<blockquote>
<p>Wenn du eine Konstante hast, wie:</p>
<pre><code>const /* constexpr wenn C++11 verfügbar */ int bit_mask = a | b | c;
</code></pre>
<p>Dann wird a | b | c ausgerechnet und in bit_mask gespeichert. Wenn a | b | c ein konstanter Ausdruck ist, wird im Endeffekt auch so etwas wie Textersetzung gemacht, nur korrekt. Da steht dann das Ergebnis von a | b | c, nicht die Operation selber.</p>
</blockquote>
<p>Aber ist das nicht rechentechnisch aufwendiger als mit einem Makro? Also Konstanten sind ja immernoch Variablen (sonst könnte man sie ja nicht per const_cast doch noch verändern) während ich mit Makros wirklich nur Werte da stehen habe?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2383344</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2383344</guid><dc:creator><![CDATA[happystudent]]></dc:creator><pubDate>Sat, 15 Feb 2014 16:27:33 GMT</pubDate></item><item><title><![CDATA[Reply to Bitmasks mit defines machen Probleme on Sat, 15 Feb 2014 16:32:10 GMT]]></title><description><![CDATA[<p>happystudent schrieb:</p>
<blockquote>
<p>Aber ist das nicht rechentechnisch aufwendiger als mit einem Makro?</p>
</blockquote>
<p>Schau dir an, was dein Compiler draus macht und staune... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2383347</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2383347</guid><dc:creator><![CDATA[dot]]></dc:creator><pubDate>Sat, 15 Feb 2014 16:32:10 GMT</pubDate></item><item><title><![CDATA[Reply to Bitmasks mit defines machen Probleme on Sat, 15 Feb 2014 16:35:23 GMT]]></title><description><![CDATA[<p>c++11fanatiker schrieb:</p>
<blockquote>
<p>Nathan schrieb:</p>
<blockquote>
<p>Wenn allerdings aus <a href="http://ideone.com/HMK6CR" rel="nofollow">irgendwelchen Gründen</a> das rechts der Zuweisung kein konstanter Ausdruck ist, gibt es mit constexpr direkt einen Fehler.</p>
</blockquote>
<p>Überzeugt mich nicht. 1. Wieso sollte ich Konstanten erst später deklarieren? 2. Der Compiler kann das soweiso optimieren, falls die Konstante nicht in constexpr-Ausdrücken gebraucht wird, erhalte ich auch keine Fehlermeldung. Und falls doch, dann habe ich eine, aber die hätte ich mit constexpr auch.</p>
</blockquote>
<p>1. Wieso sollte man einer Funktion, die einen non-nullptr erwartet einen nullptr übergeben?<br />
2. const sagt: Das ist eine Konstante, die darfste nicht verändern.<br />
constexpr sagt: Das ist ein konstanter Ausdruck, keine Variable mehr. Das wird später evtl. einfach hardgecoded in den Programmcode und belegt keinen Speicher (Antwort auch <a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/30076">@happystudent</a>).<br />
const kann das auch sagen, aber ohne Wissen über - um bei meinem Beispiel zu bleiben - was a, b und c sind, weiß man nicht, ob das ein konstanter Ausdruck ist.<br />
Die beste Dokumentation ist die, die man direkt dem Code entnehmen kann.</p>
<p>Edit <a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/30076">@happystudent</a>:<br />
Ein const_cast auf eine Variable, die ursprünglich mit const deklariert wurde, ist UB. Du kannst nur Refernenzen o.ä. auf const zu Referenzen auf nicht-const casten, und das auch nur, wenn das übergeben Objekt wirklich nicht konstant ist, da der Compiler die constexpr-Optimierung schon gemacht haben könnte, und die Variable keine Adresse mehr hat.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2383349</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2383349</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sat, 15 Feb 2014 16:35:23 GMT</pubDate></item><item><title><![CDATA[Reply to Bitmasks mit defines machen Probleme on Sat, 15 Feb 2014 16:58:15 GMT]]></title><description><![CDATA[<p>happystudent schrieb:</p>
<blockquote>
<p>Also Konstanten sind ja immernoch Variablen (sonst könnte man sie ja nicht per const_cast doch noch verändern) während ich mit Makros wirklich nur Werte da stehen habe?</p>
</blockquote>
<p>Nö. Der const_cast geht doch nur, wenn die Variable in Wirklichkeit eine variable Variable ist. Tust Du sie schon als const erzeugen, kann der Compiler sie z.B. ins ROM schreiben oder wasauchimmer machen, was man mit konstanden Variablen so macht, üblicherweise den Speicher wegoptimieren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2383352</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2383352</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sat, 15 Feb 2014 16:58:15 GMT</pubDate></item><item><title><![CDATA[Reply to Bitmasks mit defines machen Probleme on Sat, 15 Feb 2014 19:00:34 GMT]]></title><description><![CDATA[<p>Ok, danke euch alle, wieder einiges gelernt <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
<p>Btw:</p>
<p>Nathan schrieb:</p>
<blockquote>
<p>Ein const_cast auf eine Variable, die ursprünglich mit const deklariert wurde, ist UB.</p>
</blockquote>
<p>Was bedeutet &quot;UB&quot;?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2383372</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2383372</guid><dc:creator><![CDATA[happystudent]]></dc:creator><pubDate>Sat, 15 Feb 2014 19:00:34 GMT</pubDate></item><item><title><![CDATA[Reply to Bitmasks mit defines machen Probleme on Sat, 15 Feb 2014 19:11:45 GMT]]></title><description><![CDATA[<p>UB steht für Undefined Behaviour.<br />
Das komplette Programmverhalten ist somit nicht mehr im Standard definiert.<br />
Um das Standardbeispiel aufzugreifen: Der Compiler könnte an der Stelle Code generieren, der deine Festplatte formatiert und wäre komplett standardkonform.<br />
In der Regel wird das Programm aber einfach crashen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2383373</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2383373</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sat, 15 Feb 2014 19:11:45 GMT</pubDate></item><item><title><![CDATA[Reply to Bitmasks mit defines machen Probleme on Sat, 15 Feb 2014 19:29:24 GMT]]></title><description><![CDATA[<p>Dazu noch unbedingt zu sagen:<br />
Der Compiler dürfte bei UB auch bewirken, daß Dämonen aus deiner Nase fliegen!<br />
<a href="http://www.catb.org/jargon/html/N/nasal-demons.html" rel="nofollow">http://www.catb.org/jargon/html/N/nasal-demons.html</a></p>
<p>Haha, Rätselaufgabe:</p>
<pre><code>int souls=5;
int deamons=++souls+souls++;
cout&lt;&lt;deamons;
</code></pre>
<p>Wieviele Dämonen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2383376</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2383376</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sat, 15 Feb 2014 19:29:24 GMT</pubDate></item><item><title><![CDATA[Reply to Bitmasks mit defines machen Probleme on Sat, 15 Feb 2014 19:32:12 GMT]]></title><description><![CDATA[<p>volkard schrieb:</p>
<blockquote>
<p>Wieviele Dämonen?</p>
</blockquote>
<p>Nur einer. Einmal UB, immer UB.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2383378</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2383378</guid><dc:creator><![CDATA[ardve]]></dc:creator><pubDate>Sat, 15 Feb 2014 19:32:12 GMT</pubDate></item></channel></rss>