<?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[Kleiner Coding-Contest]]></title><description><![CDATA[<p>Was ist die effizienteste Methode um zu prüfen ob zwei 32-Bit words jeweils genau ein Bit gesetzt haben, und diese Bits verschieden sind?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/326445/kleiner-coding-contest</link><generator>RSS for Node</generator><lastBuildDate>Mon, 25 May 2026 06:26:13 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/326445.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 18 Jun 2014 13:28:42 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Kleiner Coding-Contest on Wed, 18 Jun 2014 20:52:56 GMT]]></title><description><![CDATA[<p>Was ist die effizienteste Methode um zu prüfen ob zwei 32-Bit words jeweils genau ein Bit gesetzt haben, und diese Bits verschieden sind?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2404437</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404437</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Wed, 18 Jun 2014 20:52:56 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner Coding-Contest on Wed, 18 Jun 2014 14:07:21 GMT]]></title><description><![CDATA[<p>Wie können zwei gesetzte Bits verschieden sein?<br />
Ansonsten Google &quot;c++ bit vergleich&quot;</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2404443</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404443</guid><dc:creator><![CDATA[Helmut.Jakoby]]></dc:creator><pubDate>Wed, 18 Jun 2014 14:07:21 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner Coding-Contest on Wed, 18 Jun 2014 14:10:39 GMT]]></title><description><![CDATA[<p>Helmut.Jakoby schrieb:</p>
<blockquote>
<p>Wie können zwei gesetzte Bits verschieden sein?</p>
</blockquote>
<pre><code>001000
010000
</code></pre>
<p>Siehst du den Unterschied?</p>
<p>Damit wird dein Tip auch wertlos.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2404444</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404444</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Wed, 18 Jun 2014 14:10:39 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner Coding-Contest on Wed, 18 Jun 2014 14:12:53 GMT]]></title><description><![CDATA[<p>in 4 bytes teilen, da kann man viel schneller drüber checken. und den bit vergleich musst du dann nur noch auf den 8 bit des bytes machen und in vielen situationen weisst du ja anhand der bytes schon dass nicht das gleiche bit oder mehr als 1 bit gesetzt ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2404445</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404445</guid><dc:creator><![CDATA[Shade Of Mine]]></dc:creator><pubDate>Wed, 18 Jun 2014 14:12:53 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner Coding-Contest on Wed, 18 Jun 2014 14:15:43 GMT]]></title><description><![CDATA[<p>Helmut.Jakoby schrieb:</p>
<blockquote>
<p>Wie können zwei gesetzte Bits verschieden sein?</p>
</blockquote>
<p>4 und 8 haben je nur ein Bit gesetzt, aber nicht das selbe Bit.</p>
<p>EDIT: OK, zu langsam.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2404447</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404447</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Wed, 18 Jun 2014 14:15:43 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner Coding-Contest on Wed, 18 Jun 2014 14:24:31 GMT]]></title><description><![CDATA[<p>Mit</p>
<pre><code>a!=0 &amp;&amp; (a &amp; (a-1))==0
</code></pre>
<p>muesste man schnell ueberpruefen koennen, ob genau ein bit gesetzt ist (nur kurze Ueberlegung, bitte um Bestaetigung).<br />
Dann muss man noch ueberpruefen, ob die integer verschieden sind. Da es bitwise gemacht wird, kann, koennte man es mit (a &amp; b)==0 schneller machen. Ich bin mir nicht sicher, ob die Compileroptimierer das selber koennen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2404450</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404450</guid><dc:creator><![CDATA[Marthog]]></dc:creator><pubDate>Wed, 18 Jun 2014 14:24:31 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner Coding-Contest on Wed, 18 Jun 2014 14:32:23 GMT]]></title><description><![CDATA[<blockquote>
<p>bitte um Bestaetigung</p>
</blockquote>
<p>Ja, das funktioniert.</p>
<p>Die erste funktionierende Lösung die mir eingefallen ist:</p>
<pre><code>unsigned u = 0b00000000000000000000000000000000,
	         v = 0b00000000000000010000000000000000;

	bool B = !(u &amp; (u - 1))
	      &amp;&amp; !(v &amp; (v - 1))
	      &amp;&amp; u != v
	      &amp;&amp; u &amp;&amp; v;
</code></pre>
<p>Ist das aber wirklich schon die effizienteste Lösung? Kann ich gar nicht fassen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2404453</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404453</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Wed, 18 Jun 2014 14:32:23 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner Coding-Contest on Wed, 18 Jun 2014 15:03:05 GMT]]></title><description><![CDATA[<p>Durch die Wahl geeigneter Befehle, kann der compiler noch viele der Berechnungen in einem Schritt erledigen. Z.B. kann er statt expliziter ueberpruefung, ob die Zahl 0 ist, auch nachschauen, ob es in der Subtraktion einen ueberlauf gab.</p>
<p>Ich komme auf 16 Assemblerbefehle, die meisten davon auch nur sehr schnelle Bitvergleiche.</p>
<pre><code>; parameter1: eax, parameter2: ebx

mov ecx, eax
sub eax, 1
jo RET_FALSE		 ; jump if overflow
test eax, ecx      ; bitwise compare
jnz RET_FALSE	; jump if not zero

mov ecx, ebx
sub ecx, 1
jo RET_FALSE		 ; jump if overflow
test ebx, ecx
jnz RET_FALSE	; jump if not zero

test eax, ebx		; bitwise and
jnz RET_FALSE
mov eax, 1
retn

RET_FALSE:
	mov eax, 0
	retn
</code></pre>
<p>macht 38 bytes binary</p>
<p>Vielleicht bekommt man es durch anderen Befehl, andere Registerwahl oder andere Reihenfolge noch ein bisschen schneller, aber ich bezweifel, dass man wirklich weniger schafft.<br />
Nur die vielen Spruenge stoeren vielleicht, wenn die Branch-Prediction falsch liegt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2404455</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404455</guid><dc:creator><![CDATA[Marthog]]></dc:creator><pubDate>Wed, 18 Jun 2014 15:03:05 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner Coding-Contest on Wed, 18 Jun 2014 15:05:21 GMT]]></title><description><![CDATA[<p>Und wie kann man damit jetzt was hacken?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2404456</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404456</guid><dc:creator><![CDATA[hardware]]></dc:creator><pubDate>Wed, 18 Jun 2014 15:05:21 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner Coding-Contest on Wed, 18 Jun 2014 16:59:49 GMT]]></title><description><![CDATA[<blockquote>
<p>Und wie kann man damit jetzt was hacken?</p>
</blockquote>
<p>In die Eingabeaufforderung eingeben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2404465</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404465</guid><dc:creator><![CDATA[Thilo87]]></dc:creator><pubDate>Wed, 18 Jun 2014 16:59:49 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner Coding-Contest on Wed, 18 Jun 2014 18:43:48 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/23821">@Marthog</a><br />
Ich denke das erste <code>sub eax, 1</code> sollte <code>sub ecx, 1</code> heissen -- so wie im 2. Block halt auch.<br />
Sonst vergleicht das <code>test eax, ebx</code> zum Schluss die falschen Werte.</p>
<p>Und wieso <code>test eax, ebx</code> zum Schluss und nicht <code>cmp</code> bzw. <code>sub</code> ? Ist <code>test</code> in so einem Fall schneller?</p>
<p>EDIT: Grammatik + Tippfehler.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2404475</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404475</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Wed, 18 Jun 2014 18:43:48 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner Coding-Contest on Wed, 18 Jun 2014 21:39:08 GMT]]></title><description><![CDATA[<p>ps: Compiler sind schlauer als Menschen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
<pre><code class="language-cpp">bool foo(unsigned u, unsigned v) {
    return !(u &amp; (u - 1)) 
		&amp;&amp; !(v &amp; (v - 1)) 
		&amp;&amp; u != v 
		&amp;&amp; u &amp;&amp; v;
}

// EDIT: Unsinn. Mein misglückter Versuch den Trick den Marthog in seinem Assembler-Beispiel verwendet hat in C++ nachzubilden.
//       Mit dem Hintergedanken dass ein C++ Compiler dadurch vielleicht besseren Code erzeugen könnte.
//       Funktioniert aber eben nur wenn u und v beide &gt;= 0 sind, d.h. das MSB nicht verwendet wird.
//       =&gt; Nicht weiter beachten.
bool bar(int u, int v) {
	int temp1 = u - 1;
	int temp2 = v - 1;

	return temp1 &gt;= 0 &amp;&amp; !(temp1 &amp; u)    //EDIT: (temp1 &amp; u) =&gt; !(temp1 &amp; u)
		&amp;&amp; temp2 &gt;= 0 &amp;&amp; !(temp2 &amp; v)    //EDIT: (temp2 &amp; v) =&gt; !(temp2 &amp; v)
		&amp;&amp; u != v;
}
</code></pre>
<p>GCC 4.9:</p>
<pre><code>foo(unsigned int, unsigned int):
	leal	-1(%rdi), %edx
	xorl	%eax, %eax
	testl	%edi, %edx
	jne	.L2
	leal	-1(%rsi), %edx
	testl	%esi, %edx
	jne	.L2
	cmpl	%esi, %edi
	setne	%dl
	testl	%edi, %edi
	setne	%al
	andl	%edx, %eax
	testl	%esi, %esi
	setne	%dl
	andl	%edx, %eax
.L2:
	rep ret

bar(int, int):
	movl	%edi, %edx
	xorl	%eax, %eax
	subl	$1, %edx
	js	.L7
	leal	-1(%rsi), %ecx
	testl	%edi, %edx
	sete	%dl
	movl	%ecx, %eax
	notl	%eax
	shrl	$31, %eax
	andb	%dl, %al
	je	.L7
	testl	%esi, %ecx
	sete	%dl
	cmpl	%esi, %edi
	setne	%al
	andl	%edx, %eax
.L7:
	rep ret
</code></pre>
<p>Nur zwei bedingte Sprünge, booyah!</p>
<p>Clang 3.4.1</p>
<pre><code>foo(unsigned int, unsigned int):                               # @foo(unsigned int, unsigned int)
	leal	-1(%rdi), %eax
	testl	%edi, %eax
	jne	.LBB0_3
	leal	-1(%rsi), %eax
	testl	%esi, %eax
	je	.LBB0_2
.LBB0_3:
	xorl	%eax, %eax
	ret
.LBB0_2:
	cmpl	%esi, %edi
	setne	%al
	testl	%edi, %edi
	setne	%cl
	andb	%al, %cl
	testl	%esi, %esi
	setne	%al
	andb	%cl, %al
	ret

bar(int, int):                               # @bar(int, int)
	testl	%edi, %edi
	jle	.LBB1_2
	leal	-1(%rsi), %eax
	leal	-1(%rdi), %ecx
	testl	%edi, %ecx
	sete	%cl
	testl	%esi, %esi
	setg	%dl
	andb	%cl, %dl
	testl	%esi, %eax
	sete	%cl
	andb	%dl, %cl
	cmpl	%esi, %edi
	setne	%al
	andb	%cl, %al
	ret
.LBB1_2:
	xorl	%eax, %eax
	ret
</code></pre>
<p>Nur zwei bedingte Sprünge bzw. bei bar sogar nur einer. BOOYAH!</p>
<p>ICC 13.0.1</p>
<pre><code>L__routine_start__Z3foojj_0:
foo(unsigned int, unsigned int):
        lea       -1(%rdi), %eax                                #2.23
        testl     %eax, %edi                                    #2.23
        jne       ..B1.6        # Prob 50%                      #2.23
        lea       -1(%rsi), %eax                                #3.17
        testl     %eax, %esi                                    #3.17
        jne       ..B1.6        # Prob 50%                      #3.17
        cmpl      %esi, %edi                                    #4.11
        je        ..B1.6        # Prob 50%                      #4.11
        testl     %edi, %edi                                    #5.6
        je        ..B1.6        # Prob 50%                      #5.6
        movl      $1, %eax                                      #2.23
        testl     %esi, %esi                                    #2.23
        cmove     %esi, %eax                                    #2.23
        ret                                                     #2.23
..B1.6:                         # Preds ..B1.4 ..B1.1 ..B1.2 ..B1.3
        xorl      %eax, %eax                                    #2.23
        ret                                                     #2.23

L__routine_start__Z3barii_1:
bar(int, int):
        movl      %edi, %eax                                    #9.18
        decl      %eax                                          #9.18
        lea       -1(%rsi), %edx                                #10.18
        js        ..B2.6        # Prob 16%                      #12.18
        testl     %eax, %edi                                    #12.33
        jne       ..B2.6        # Prob 50%                      #12.33
        testl     %edx, %edx                                    #13.15
        js        ..B2.6        # Prob 16%                      #13.15
        testl     %edx, %esi                                    #13.30
        jne       ..B2.6        # Prob 50%                      #13.30
        movl      $1, %edx                                      #12.18
        xorl      %eax, %eax                                    #12.18
        cmpl      %esi, %edi                                    #12.18
        cmovne    %edx, %eax                                    #12.18
        ret                                                     #12.18
..B2.6:                         # Preds ..B2.4 ..B2.1 ..B2.2 ..B2.3
        xorl      %eax, %eax                                    #12.18
        ret                                                     #12.18
</code></pre>
<p>4 bedingte Sprünge. Hmpf.</p>
<p>ps: Wer die Seite nicht kennt: <a href="http://gcc.godbolt.org/" rel="nofollow">http://gcc.godbolt.org/</a><br />
Sehr cooles Tool wenn man schnell mal den ASM Output verschiedener Compiler vergleichen will.</p>
<p>EDIT: Fehler in &quot;bar&quot; korrigiert, ASM Output entsprechend upgedated (die Anzahl der jcc hat sich dadurch nicht verändert).</p>
<p>ps2: <code>bar</code> funktioniert natürlich nur wenn das MSB Bit immer 0 ist. Fällt mir auch jetzt erst auf, manchmal steh ich ganz schön aufm Schlauch <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>EDIT: Kommentar über die (nicht vorhandene) Sinnhaftigkeit von &quot;bar&quot; hinzugefügt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2404479</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404479</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Wed, 18 Jun 2014 21:39:08 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner Coding-Contest on Wed, 18 Jun 2014 18:25:34 GMT]]></title><description><![CDATA[<p>hustbaer schrieb:</p>
<blockquote>
<p>Ich denke das erste <code>sub eax, 1</code> sollte <code>sub ecx, 1</code> heissen -- so wie im 1. Block halt auch.<br />
Sonst vergleicht das <code>test eax, ebx</code> zum Schluss die falschen Werte.</p>
<p>Und wieso <code>test eax, ebx</code> zum Schluss und nicht <code>cmp</code> bzw. <code>sub</code> ? Ist <code>test</code> schneller in so einem Fall schneller?</p>
</blockquote>
<p>Beim ersten hast du recht.<br />
cmp subtrahiert ja die Werte, verwirft das Ergebnis und setzt die flags. Test macht ein bitand, verwirft das Ergebnis und setzt die flags. Bitwise-and ist deutlich einfacher zu implementieren. Ich weiss nicht, ob test heute noch schneller ist, aber frueher hat man das immer so gemacht und Compiler uebersetzen heutzutage auch <em>x==0</em> meistens mit <em>test eax, eax</em> statt mit <em>cmp eax, 0</em>.<br />
In diesem Fall hat man nur zwei Moeglichkeiten, naemlich das bit ist an der gleichen Stelle (bitand ergibt nicht 0) und das bit ist verschoben (bitand ergibt 0) und man kann test problemlos einsetzen.</p>
<p>EDIT: Achja, der gute lea-Befehl. Mit dem uebertrumpfen mich die Compiler jedes Mal. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2404480</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404480</guid><dc:creator><![CDATA[Marthog]]></dc:creator><pubDate>Wed, 18 Jun 2014 18:25:34 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner Coding-Contest on Wed, 18 Jun 2014 18:25:25 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/23821">@Marthog</a><br />
Dass man es verwenden kann hab ich schon gecheckt, bin ja ein Schlauer <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /><br />
Ich hätte dort halt vermutlich einfach <code>cmp</code> verwendet, weil ich es mit <code>cmp</code> einfacher zu verstehen finde was abgeht. Und daher hab' ich nachgefragt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2404482</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404482</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Wed, 18 Jun 2014 18:25:25 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner Coding-Contest on Wed, 18 Jun 2014 18:44:31 GMT]]></title><description><![CDATA[<p>Und ich meinte natürlich &quot;so wie im 2. Block halt auch&quot;. Aber du bist ja auch ein Schlauer und hast es trotzdem verstanden <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>
]]></description><link>https://www.c-plusplus.net/forum/post/2404485</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404485</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Wed, 18 Jun 2014 18:44:31 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner Coding-Contest on Wed, 18 Jun 2014 20:41:08 GMT]]></title><description><![CDATA[<p>Toller Hacker Contest. <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>Und morgen kommt der Wettbewerb &quot;Wer schreibt zuerst ein kompilierbares Program&quot;.</p>
<p>WinAPI Forum lässt grüßen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2404501</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404501</guid><dc:creator><![CDATA[Bitte ein Bit]]></dc:creator><pubDate>Wed, 18 Jun 2014 20:41:08 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner Coding-Contest on Wed, 18 Jun 2014 20:49:21 GMT]]></title><description><![CDATA[<p>Der Threadtitel bezieht sich nicht aufs Hacken, sondern auf den alten Forumsnamen von Arcoth. Und klein wahrscheinlich weil er noch ein Schüler ist/war.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2404502</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404502</guid><dc:creator><![CDATA[Mechanics]]></dc:creator><pubDate>Wed, 18 Jun 2014 20:49:21 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner Coding-Contest on Wed, 18 Jun 2014 20:58:41 GMT]]></title><description><![CDATA[<p>Mechanics schrieb:</p>
<blockquote>
<p>Der Threadtitel bezieht sich nicht aufs Hacken, sondern auf den alten Forumsnamen von Arcoth.</p>
</blockquote>
<p>Völlig falsch.</p>
<blockquote>
<p>Und klein wahrscheinlich weil er noch ein Schüler ist/war.</p>
</blockquote>
<p>Ebenfalls völliger Unsinn.</p>
<p>Ich habe den Titel entsprechend angepasst... hier muss man wirklich das niedrigste Niveau voraussehen.</p>
<p>Edit: @Mehanics: Ich nehme an, dass du das nicht ernst gemeint hast?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2404504</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404504</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Wed, 18 Jun 2014 20:58:41 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner Coding-Contest on Wed, 18 Jun 2014 21:16:32 GMT]]></title><description><![CDATA[<p>Ich verstehe denn code nicht kann ihn mir jemand kurz erklären?</p>
<pre><code>bool foo(unsigned u, unsigned v) {
    return !(u &amp; (u - 1)) // bitweise UND sagt einen doch ob u und u-1 beide ein bit an der gleiche position auf 1 gesetzt haben?
        &amp;&amp; !(v &amp; (v - 1)) // gleiche
        &amp;&amp; u != v // u ist nicht gleich v
        &amp;&amp; u &amp;&amp; v; // das hier verstehe ich nicht :/
}

bool bar(int u, int v) {
    int temp1 = u - 1;
    int temp2 = v - 1;

    return temp1 &gt;= 0 &amp;&amp; !(temp1 &amp; u)    //EDIT: (temp1 &amp; u) =&gt; !(temp1 &amp; u)
        &amp;&amp; temp2 &gt;= 0 &amp;&amp; !(temp2 &amp; v)    //EDIT: (temp2 &amp; v) =&gt; !(temp2 &amp; v)
        &amp;&amp; u != v;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2404509</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404509</guid><dc:creator><![CDATA[theconflict]]></dc:creator><pubDate>Wed, 18 Jun 2014 21:16:32 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner Coding-Contest on Wed, 18 Jun 2014 21:17:52 GMT]]></title><description><![CDATA[<p>Ach, Leute...</p>
<p><a href="http://www.catb.org/jargon/html/H/hacker.html" rel="nofollow">http://www.catb.org/jargon/html/H/hacker.html</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2404510</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404510</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Wed, 18 Jun 2014 21:17:52 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner Coding-Contest on Wed, 18 Jun 2014 21:27:28 GMT]]></title><description><![CDATA[<p>Arcoth schrieb:</p>
<blockquote>
<p>Edit: @Mehanics: Ich nehme an, dass du das nicht ernst gemeint hast?</p>
</blockquote>
<p>Natürlich nicht. Sorry, ich konnte einfach nicht widerstehen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2404515</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404515</guid><dc:creator><![CDATA[Mechanics]]></dc:creator><pubDate>Wed, 18 Jun 2014 21:27:28 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner Coding-Contest on Wed, 18 Jun 2014 21:30:26 GMT]]></title><description><![CDATA[<p>theconflict schrieb:</p>
<blockquote>
<p>Ich verstehe denn code nicht kann ihn mir jemand kurz erklären?</p>
<pre><code>bool foo(unsigned u, unsigned v) {
    return !(u &amp; (u - 1)) // bitweise UND sagt einen doch ob u und u-1 beide ein bit an der gleiche position auf 1 gesetzt haben?
        &amp;&amp; !(v &amp; (v - 1)) // gleiche
        &amp;&amp; u != v // u ist nicht gleich v
        &amp;&amp; u &amp;&amp; v; // das hier verstehe ich nicht :/
}
</code></pre>
</blockquote>
<p><code>(u &amp; (u - 1))</code><br />
Wenn du dir &quot;u - 1&quot; binär anguckst, wirst du feststellen, dass dabei das erste 1er Bit (=das mit dem niedrigsten Stellenwert) zu 0 geflippt wird, und alle 0er Bits davor werden zu 1 geflippt:</p>
<pre><code>u           u - 1
--------------------
xxxxxxx1    xxxxxxx0
xxxxxx10    xxxxxx01
xxxxx100    xxxxx011
xxxx1000    xxxx0111
</code></pre>
<p>etc.<br />
Wenn man jetzt die linke und rechte Seite bitweise verundet, dann kommt dabei raus:</p>
<pre><code>u           u - 1       u &amp; (u - 1)
-------------------------------------------
xxxxxxx1    xxxxxxx0    xxxxxxx0
xxxxxx10    xxxxxx01    xxxxxx00
xxxxx100    xxxxx011    xxxxx000
xxxx1000    xxxx0111    xxxx0000
</code></pre>
<p>D.h. <code>u &amp; (u - 1)</code> setzt einfach das erste 1er Bit auf 0.</p>
<p>Und wenn das Ergebnis von &quot;erstes 1er Bit auf 0 setzen&quot; 0 ist, dann war vorher maximal ein Bit gesetzt.</p>
<p>D.h. <code>!(u &amp; (u - 1))</code> heisst: hat u genau ein 1er Bit oder ist u gleich 0?</p>
<p>Den &quot;oder ist u gleich 0&quot; Teil will man aber nicht, es sind ja nur Werte erwünscht die genau ein 1er Bit haben.<br />
=&gt; <code>!(u &amp; (u - 1)) &amp;&amp; u</code></p>
<p>Das ganze dann nochmal für <code>v</code> , und dann noch den Test ob u und v ungleich sind drangehängt:</p>
<pre><code>!(u &amp; (u - 1))
&amp;&amp; u
&amp;&amp; !(v &amp; (v - 1))
&amp;&amp; v
&amp;&amp; u != v
</code></pre>
<p>Etwas umsortiert:</p>
<pre><code>!(u &amp; (u - 1))
&amp;&amp; !(v &amp; (v - 1))
&amp;&amp; u != v
&amp;&amp; u &amp;&amp; v
</code></pre>
<p>Tadaaa.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2404517</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404517</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Wed, 18 Jun 2014 21:30:26 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner Coding-Contest on Wed, 18 Jun 2014 21:43:14 GMT]]></title><description><![CDATA[<p>Danke jetzt habe ich es verstanden :}</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2404520</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404520</guid><dc:creator><![CDATA[theconflict]]></dc:creator><pubDate>Wed, 18 Jun 2014 21:43:14 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner Coding-Contest on Fri, 20 Jun 2014 13:32:42 GMT]]></title><description><![CDATA[<p>Aufbauend auf hustbaer's Ansatz komme ich auf folgendes:</p>
<pre><code>bool foo2(unsigned u, unsigned v) { 
    return !(u &amp; (u - 1)) 
    &amp;&amp; !(v &amp; (v - 1)) 
    &amp;&amp; (u | v) != u
    &amp;&amp; u;
}
</code></pre>
<p>So macht der Clang nur noch einen Sprung <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>
<p>Warum reicht es? Wenn das bitweise Oder von u und v wieder u ergibt, gilt entweder u == v oder v == 0. Es bleibt also zu überprüfen, ob u == 0 ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2404741</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404741</guid><dc:creator><![CDATA[und_u]]></dc:creator><pubDate>Fri, 20 Jun 2014 13:32:42 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner Coding-Contest on Fri, 20 Jun 2014 13:42:11 GMT]]></title><description><![CDATA[<p>Anmerkung: Streicht das &quot;entweder&quot; in meiner Erklärung, es kann auch der Fall eintreten, das u == v == 0 ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2404742</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404742</guid><dc:creator><![CDATA[und_u]]></dc:creator><pubDate>Fri, 20 Jun 2014 13:42:11 GMT</pubDate></item><item><title><![CDATA[Reply to Kleiner Coding-Contest on Sat, 21 Jun 2014 11:32:44 GMT]]></title><description><![CDATA[<p>Um mal dem niedrigen Niveau gerecht zu werden: <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>
<p>Ich habe mal ein andere Idee für die Funktion ob ein<br />
unsigned long exakt ein Bit gesetzt hat. Hierzu wird<br />
der Wert in 4 Bytes unterteilt. Und jedes Byte in die<br />
oberen und unteren 4 Bits. Für jede 4 Bits stellen<br />
wir eine boolsche Funktion auf und minimieren diese<br />
mittels einem KV Diagramm. Die ergebende Formel F sagt<br />
uns ob nur ein Bit gesetzt wurde. Um nun zu prüfen<br />
ob nun ein Bit in einem Byte gesetzt ist, testet man<br />
ob entweder in den oberen 4 Bits ein Bit gesetzt wurde,<br />
oder in den unteren Bits. Also F(x) xor F(x 》4).<br />
Über die xor Verknüpfung kann entsprechend die 4 Bytes<br />
getestet werden.</p>
<p>Alternativ könnte man auch ein künstlich neuronales Netz<br />
ausprobieren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2404897</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404897</guid><dc:creator><![CDATA[Bitte ein Bit]]></dc:creator><pubDate>Sat, 21 Jun 2014 11:32:44 GMT</pubDate></item></channel></rss>