<?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[frage zu bestimmtes bit setzen]]></title><description><![CDATA[<p>hallo,<br />
ich schreibe gerade ein poker-programm in C++ und haette gerne gewusst, ob ihr ne Idee zu folgendem Bit-Setz-Problem habt:</p>
<p>in einem Bitmuster (welches nicht null ist) soll ein bestimmtes Null-Bit gesetzt werden, undzwar dasjenige welches am weitesten links (most significant bit) liegt es jedoch noch gesetzte Bits links gibt:</p>
<p>Beispiel(e)</p>
<p>0011 0110 --&gt; 0011 1110<br />
1011 0110 --&gt; 1111 0110<br />
0000 0110 --&gt; 0000 0111</p>
<p>vielen dank.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/224882/frage-zu-bestimmtes-bit-setzen</link><generator>RSS for Node</generator><lastBuildDate>Fri, 02 Oct 2026 03:29:34 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/224882.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 16 Oct 2008 10:26:49 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to frage zu bestimmtes bit setzen on Thu, 16 Oct 2008 10:26:49 GMT]]></title><description><![CDATA[<p>hallo,<br />
ich schreibe gerade ein poker-programm in C++ und haette gerne gewusst, ob ihr ne Idee zu folgendem Bit-Setz-Problem habt:</p>
<p>in einem Bitmuster (welches nicht null ist) soll ein bestimmtes Null-Bit gesetzt werden, undzwar dasjenige welches am weitesten links (most significant bit) liegt es jedoch noch gesetzte Bits links gibt:</p>
<p>Beispiel(e)</p>
<p>0011 0110 --&gt; 0011 1110<br />
1011 0110 --&gt; 1111 0110<br />
0000 0110 --&gt; 0000 0111</p>
<p>vielen dank.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1599833</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1599833</guid><dc:creator><![CDATA[pepe75]]></dc:creator><pubDate>Thu, 16 Oct 2008 10:26:49 GMT</pubDate></item><item><title><![CDATA[Reply to frage zu bestimmtes bit setzen on Thu, 16 Oct 2008 10:28:21 GMT]]></title><description><![CDATA[<p>eine zusaetzliche anforderung ist es,<br />
das ganze moeglichst schnell durchzufuehren, ein ellen-langer-algo waere nicht so toll.<br />
danke.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1599835</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1599835</guid><dc:creator><![CDATA[pepe75]]></dc:creator><pubDate>Thu, 16 Oct 2008 10:28:21 GMT</pubDate></item><item><title><![CDATA[Reply to frage zu bestimmtes bit setzen on Thu, 16 Oct 2008 10:31:32 GMT]]></title><description><![CDATA[<p>Und bei sowas:<br />
0100 1000?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1599838</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1599838</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Thu, 16 Oct 2008 10:31:32 GMT</pubDate></item><item><title><![CDATA[Reply to frage zu bestimmtes bit setzen on Thu, 16 Oct 2008 10:33:04 GMT]]></title><description><![CDATA[<p>ich werd aus deiner Beschreibung nicht wirklich schlau und auch deine Beispiele sind nichtssagend, da dort nirgends das MSB geaendert wird</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1599839</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1599839</guid><dc:creator><![CDATA[zwutz]]></dc:creator><pubDate>Thu, 16 Oct 2008 10:33:04 GMT</pubDate></item><item><title><![CDATA[Reply to frage zu bestimmtes bit setzen on Thu, 16 Oct 2008 10:37:37 GMT]]></title><description><![CDATA[<p>Häßlich aber schnell. Ein paar andere Algorithmen findest du <a href="http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogDeBruijn" rel="nofollow">hier</a></p>
<pre><code class="language-cpp">static const char LogTable256[] = 
{
  0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
};

unsigned int v = old_value; // 32-bit word to find the log of
unsigned r;     // r will be lg(v)
register unsigned int t, tt; // temporaries

if (tt = v &gt;&gt; 16)
{
  r = (t = tt &gt;&gt; 8) ? 24 + LogTable256[t] : 16 + LogTable256[tt];
}
else 
{
  r = (t = v &gt;&gt; 8) ? 8 + LogTable256[t] : LogTable256[v];
}

unsigned int new_value;
do{
  new_value = old_value | pow(2, (--r));
while(r &gt;= 0 &amp;&amp; new_value == old_value);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1599843</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1599843</guid><dc:creator><![CDATA[Fellhuhn]]></dc:creator><pubDate>Thu, 16 Oct 2008 10:37:37 GMT</pubDate></item><item><title><![CDATA[Reply to frage zu bestimmtes bit setzen on Thu, 16 Oct 2008 10:48:41 GMT]]></title><description><![CDATA[<p>Tachyon schrieb:</p>
<blockquote>
<p>Und bei sowas:<br />
0100 1000?</p>
</blockquote>
<p>wuerde 0110 1000 ergeben.</p>
<p>zwutz schrieb:</p>
<blockquote>
<p>ich werd aus deiner Beschreibung nicht wirklich schlau und auch deine Beispiele sind nichtssagend, da dort nirgends das MSB geaendert wird</p>
</blockquote>
<p>soll ja auch nicht. die beschreibung welches bit gesetzt werden soll ist in der tat umgangsprachlich etwas schwierig, versuche es aber nochmal <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<p>Finde die am weitesten links liegende Bitposition an der ein Null-Bit vorzufinden ist, jedoch links von dieser position mindestens noch eine 1 vorzufinden ist.</p>
<p>ich gehe hier beispielhaft von 32-bit-zahlen aus und davon dass das zu untersuchende bitmuster zahlenmaessig keine Null ist.</p>
<p>danke.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1599852</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1599852</guid><dc:creator><![CDATA[pepe75]]></dc:creator><pubDate>Thu, 16 Oct 2008 10:48:41 GMT</pubDate></item><item><title><![CDATA[Reply to frage zu bestimmtes bit setzen on Thu, 16 Oct 2008 10:51:43 GMT]]></title><description><![CDATA[<p>Fellhuhn schrieb:</p>
<blockquote>
<p>Häßlich aber schnell. Ein paar andere Algorithmen findest du <a href="http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogDeBruijn" rel="nofollow">hier</a></p>
<pre><code class="language-cpp">static const char LogTable256[] = 
{
  0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
};

unsigned int v = old_value; // 32-bit word to find the log of
unsigned r;     // r will be lg(v)
register unsigned int t, tt; // temporaries

if (tt = v &gt;&gt; 16)
{
  r = (t = tt &gt;&gt; 8) ? 24 + LogTable256[t] : 16 + LogTable256[tt];
}
else 
{
  r = (t = v &gt;&gt; 8) ? 8 + LogTable256[t] : LogTable256[v];
}

unsigned int new_value;
do{
  new_value = old_value | pow(2, (--r));
while(r &gt;= 0 &amp;&amp; new_value == old_value);
</code></pre>
</blockquote>
<p>UPS, ... ähm, ist natuerlich schoen, wenn obiges tatsaechlich das macht was ich beschrieben habe (und das vielleicht noch schnell??). Vorraussetzung hierfuer ist jedoch das Du mein Problem verstanden hast (oder ich es ausreichend hervorbringen konnte) -&gt; macht obiges wirklich das, was ich will?!?</p>
<p>danke.<br />
gruss.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1599854</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1599854</guid><dc:creator><![CDATA[pepe75]]></dc:creator><pubDate>Thu, 16 Oct 2008 10:51:43 GMT</pubDate></item><item><title><![CDATA[Reply to frage zu bestimmtes bit setzen on Thu, 16 Oct 2008 10:56:22 GMT]]></title><description><![CDATA[<p>Sollte.</p>
<p>EDIT:</p>
<p>Im Grunde rechnet er dort log_2(value) und geht dann schrittweise die bits zurück um zu schauen ob sich noch eine 0 weiter rechts finden läßt und setzt die dann.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1599856</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1599856</guid><dc:creator><![CDATA[Fellhuhn]]></dc:creator><pubDate>Thu, 16 Oct 2008 10:56:22 GMT</pubDate></item><item><title><![CDATA[Reply to frage zu bestimmtes bit setzen on Thu, 16 Oct 2008 11:11:00 GMT]]></title><description><![CDATA[<p>jetzt blick ich durch... mich hat dein Hinweis auf das MSB irritiert, da das wirklich _nur_ das MOST Signigficant Bit bezeichnet und danach ja logischerweise nichts mehr kommen kann</p>
<p>was ist aber bei 0000 0001?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1599865</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1599865</guid><dc:creator><![CDATA[zwutz]]></dc:creator><pubDate>Thu, 16 Oct 2008 11:11:00 GMT</pubDate></item><item><title><![CDATA[Reply to frage zu bestimmtes bit setzen on Thu, 16 Oct 2008 12:08:00 GMT]]></title><description><![CDATA[<p>zwutz schrieb:</p>
<blockquote>
<p>jetzt blick ich durch... mich hat dein Hinweis auf das MSB irritiert, da das wirklich _nur_ das MOST Signigficant Bit bezeichnet und danach ja logischerweise nichts mehr kommen kann<br />
was ist aber bei 0000 0001?</p>
</blockquote>
<p>stimmt ist ein sonderfall, ich wuerd sagen dass ding bleibt unveraendert<br />
danke.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1599891</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1599891</guid><dc:creator><![CDATA[pepe75]]></dc:creator><pubDate>Thu, 16 Oct 2008 12:08:00 GMT</pubDate></item><item><title><![CDATA[Reply to frage zu bestimmtes bit setzen on Thu, 16 Oct 2008 12:16:02 GMT]]></title><description><![CDATA[<p>[quote=&quot;pepe75&quot;]<br />
UPS, ... ähm, ist natuerlich schoen, wenn obiges tatsaechlich das macht was ich beschrieben habe (und das vielleicht noch schnell??). Vorraussetzung hierfuer ist jedoch das Du mein Problem verstanden hast (oder ich es ausreichend hervorbringen konnte) -&gt; macht obiges wirklich das, was ich will?!?<br />
/quote]</p>
<p>Wenn ich das hier so schön serviert bekäme, würde ich es einfach ausprobieren, und mich melden, wenn es nicht so funktionieren würde, wie ich wollte ...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1599895</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1599895</guid><dc:creator><![CDATA[Belli]]></dc:creator><pubDate>Thu, 16 Oct 2008 12:16:02 GMT</pubDate></item><item><title><![CDATA[Reply to frage zu bestimmtes bit setzen on Thu, 16 Oct 2008 12:49:08 GMT]]></title><description><![CDATA[<p>Fellhuhn schrieb:</p>
<blockquote>
<p>Sollte.</p>
<p>EDIT:</p>
<p>Im Grunde rechnet er dort log_2(value) und geht dann schrittweise die bits zurück um zu schauen ob sich noch eine 0 weiter rechts finden läßt und setzt die dann.</p>
</blockquote>
<p>pow(2, foo ?<br />
warum enthält die LUT nicht gleich die gesuchte Bitstelle?</p>
<pre><code class="language-cpp">template &lt;unsigned v&gt;
struct strange_bit_scan
{
    static const int value = v % 2 == 0 || strange_bit_scan&lt; v / 2 &gt;::value != -1 ? 1 + strange_bit_scan&lt; v / 2 &gt;::value : -1;
};

template &lt;&gt;
struct strange_bit_scan&lt; 0 &gt;
{
    static const int value = -1;
};
</code></pre>
<p>kann man trivial zum generieren einer LUT benutzen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1599918</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1599918</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Thu, 16 Oct 2008 12:49:08 GMT</pubDate></item></channel></rss>