<?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[Vergleich zweier bit Zustände]]></title><description><![CDATA[<p>Hi! Ich habe hier 6 verschiedene Bitmasken: 0x10, 0x20, 0x40, 0x80, 0x100 und 0x200.<br />
Binär gesehen wird für die nächste Maske einfach nur ein bit weiter geschoben. Wie kann ich effizient herausfinden um wie viele bits zwei werte verschoben sind?</p>
<p>Beispiele:<br />
0x10 und 0x20 -&gt; 1<br />
0x10 und 0x40 -&gt; 2<br />
0x11 und 0x20 -&gt; 1<br />
0x11 und 0x23 -&gt; 1<br />
0x80 und 0x40 -&gt; -1<br />
0x103 und 0x42 -&gt; -2<br />
0x200 und 0x208 -&gt; 0</p>
<p>Müsste eigentlich die letzen vier bits abschneiden und dann schauen dann schauen um wie viel es verschoben ist oder? Wie mache ich das am besten mit möglichst wenig Operationen?</p>
<p>Vielen Dank!</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/343524/vergleich-zweier-bit-zustände</link><generator>RSS for Node</generator><lastBuildDate>Thu, 08 Oct 2026 13:39:08 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/343524.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 29 Jun 2017 12:34:43 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Vergleich zweier bit Zustände on Thu, 29 Jun 2017 12:34:43 GMT]]></title><description><![CDATA[<p>Hi! Ich habe hier 6 verschiedene Bitmasken: 0x10, 0x20, 0x40, 0x80, 0x100 und 0x200.<br />
Binär gesehen wird für die nächste Maske einfach nur ein bit weiter geschoben. Wie kann ich effizient herausfinden um wie viele bits zwei werte verschoben sind?</p>
<p>Beispiele:<br />
0x10 und 0x20 -&gt; 1<br />
0x10 und 0x40 -&gt; 2<br />
0x11 und 0x20 -&gt; 1<br />
0x11 und 0x23 -&gt; 1<br />
0x80 und 0x40 -&gt; -1<br />
0x103 und 0x42 -&gt; -2<br />
0x200 und 0x208 -&gt; 0</p>
<p>Müsste eigentlich die letzen vier bits abschneiden und dann schauen dann schauen um wie viel es verschoben ist oder? Wie mache ich das am besten mit möglichst wenig Operationen?</p>
<p>Vielen Dank!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2533796</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2533796</guid><dc:creator><![CDATA[Bitmaskenshift]]></dc:creator><pubDate>Thu, 29 Jun 2017 12:34:43 GMT</pubDate></item><item><title><![CDATA[Reply to Vergleich zweier bit Zustände on Thu, 29 Jun 2017 12:37:04 GMT]]></title><description><![CDATA[<p>Ist in jeder Bitmaske immer nur ein Bit gesetzt?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2533797</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2533797</guid><dc:creator><![CDATA[DocShoe]]></dc:creator><pubDate>Thu, 29 Jun 2017 12:37:04 GMT</pubDate></item><item><title><![CDATA[Reply to Vergleich zweier bit Zustände on Thu, 29 Jun 2017 12:40:01 GMT]]></title><description><![CDATA[<p>Ja.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2533798</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2533798</guid><dc:creator><![CDATA[Bitmaskenshift]]></dc:creator><pubDate>Thu, 29 Jun 2017 12:40:01 GMT</pubDate></item><item><title><![CDATA[Reply to Vergleich zweier bit Zustände on Thu, 29 Jun 2017 13:01:05 GMT]]></title><description><![CDATA[<p>Bitmaskenshift schrieb:</p>
<blockquote>
<p>Müsste eigentlich die letzen vier bits abschneiden</p>
</blockquote>
<pre><code>var = var &amp; 0xFFFFFF00;
</code></pre>
<blockquote>
<p>und dann schauen dann schauen um wie viel es verschoben ist oder? Wie mache ich das am besten mit möglichst wenig Operationen?</p>
<p>Vielen Dank!</p>
</blockquote>
<p>den größeren so lange durch 2 teilen, bis er gleich dem kleineren ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2533799</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2533799</guid><dc:creator><![CDATA[Wade1234]]></dc:creator><pubDate>Thu, 29 Jun 2017 13:01:05 GMT</pubDate></item><item><title><![CDATA[Reply to Vergleich zweier bit Zustände on Thu, 29 Jun 2017 13:05:45 GMT]]></title><description><![CDATA[<p>bevor jemand meckert: 0xFFFFFFF0 <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/2533800</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2533800</guid><dc:creator><![CDATA[Wade1234]]></dc:creator><pubDate>Thu, 29 Jun 2017 13:05:45 GMT</pubDate></item><item><title><![CDATA[Reply to Vergleich zweier bit Zustände on Thu, 29 Jun 2017 13:17:41 GMT]]></title><description><![CDATA[<pre><code>void diffTest() {
	//Anfangswerte
	int state0 = 0x11;
	int state1 = 0x43; 

	//Letzten 4 bits abschneiden
	state0 &amp;= 0x3F0;
	state1 &amp;= 0x3F0;

	//Merken ob negativ oder nicht
	bool isNegative = false;
	if (state0 &lt; state1) {
		isNegative = true;
		swap(state0, state1);
	}

	int diff = 0;
	while (state0 &gt; state1) {
		diff++;
		state0 &gt;&gt;= 1;
	}

	if (isNegative) diff *= -1;
}
</code></pre>
<p>Das geht doch bestimmt eleganter und kürzer...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2533802</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2533802</guid><dc:creator><![CDATA[Bitmaskenshift]]></dc:creator><pubDate>Thu, 29 Jun 2017 13:17:41 GMT</pubDate></item><item><title><![CDATA[Reply to Vergleich zweier bit Zustände on Thu, 29 Jun 2017 13:47:29 GMT]]></title><description><![CDATA[<p>Eleganter und kürzer wäre eine Division, weil dann log_2(Divisionsergebnis) direkt die Verschiebung ist. Schneller wird das aber nicht, eher im Gegenteil.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2533805</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2533805</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Thu, 29 Jun 2017 13:47:29 GMT</pubDate></item><item><title><![CDATA[Reply to Vergleich zweier bit Zustände on Thu, 29 Jun 2017 14:05:05 GMT]]></title><description><![CDATA[<pre><code>uint32_t diff(uint32_t a, uint32_t b)
{
	if(b &gt; a) std::swap(a, b);
	auto c = a - b;
	c = (c &amp; 0x55555555) + ((c &gt;&gt; 1) &amp; 0x55555555);
	c = (c &amp; 0x33333333) + ((c &gt;&gt; 2) &amp; 0x33333333);
	c = (c &amp; 0x0f0f0f0f) + ((c &gt;&gt; 4) &amp; 0x0f0f0f0f);
	c = (c &amp; 0x00ff00ff) + ((c &gt;&gt; 8) &amp; 0x00ff00ff);
	return (c &amp; 0x0000ffff) + ((c &gt;&gt; 16) &amp; 0x0000ffff);
}
</code></pre>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f921.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--clown_face"
      title=":clown:"
      alt="🤡"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2533806</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2533806</guid><dc:creator><![CDATA[Caligulaminus]]></dc:creator><pubDate>Thu, 29 Jun 2017 14:05:05 GMT</pubDate></item><item><title><![CDATA[Reply to Vergleich zweier bit Zustände on Thu, 29 Jun 2017 14:23:33 GMT]]></title><description><![CDATA[<pre><code>int diff(unsigned a, unsigned b) {
    static const char lut[] = {
        0, 0, 1, 0, 2, 1, 9, 0,
        3, 2, 9, 1, 9, 9, 9, 0,
        4, 3, 9, 2, 9, 9, 9, 1,
        9, 9, 9, 9, 9, 9, 9, 0,
        5, 4, 9, 3, 9, 9, 9, 2,
        9, 9, 9, 9, 9, 9, 9, 1 };

    a &amp;= 0x3f0;
    b &amp;= 0x3f0;
    assert((a | b) != 0 &amp;&amp; lut[(a | b) / 16 - 1] != 9);

    return a &lt; b ? -lut[(a | b) / 16 - 1] : lut[(a | b) / 16 - 1];
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2533809</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2533809</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Thu, 29 Jun 2017 14:23:33 GMT</pubDate></item></channel></rss>