<?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[Reihenfolge der Addition]]></title><description><![CDATA[<p><strong>Hallo</strong></p>
<p>Ich habe zwei Funktionen geschrieben, die eigentlich exakt denselben Wert zurückgeben sollten:</p>
<pre><code class="language-cpp">key_t board_c::generate_random_key_1(void) {
  key_t result;

  result = 0;

  result += key_t(rand() &amp; 15);
  result += key_t(rand() &amp; 15) &lt;&lt; 4;
  result += key_t(rand() &amp; 15) &lt;&lt; 8;
  result += key_t(rand() &amp; 15) &lt;&lt; 12;
  result += key_t(rand() &amp; 15) &lt;&lt; 16;
  result += key_t(rand() &amp; 15) &lt;&lt; 20;
  result += key_t(rand() &amp; 15) &lt;&lt; 24;
  result += key_t(rand() &amp; 15) &lt;&lt; 28;
  result += key_t(rand() &amp; 15) &lt;&lt; 32;
  result += key_t(rand() &amp; 15) &lt;&lt; 36;
  result += key_t(rand() &amp; 15) &lt;&lt; 40;
  result += key_t(rand() &amp; 15) &lt;&lt; 44;
  result += key_t(rand() &amp; 15) &lt;&lt; 48;
  result += key_t(rand() &amp; 15) &lt;&lt; 52;
  result += key_t(rand() &amp; 15) &lt;&lt; 56;
  result += key_t(rand() &amp; 15) &lt;&lt; 60;

  return result;
}

key_t board_c::generate_random_key_2(void) {
  return
    (key_t(rand() &amp; 15)) +
    (key_t(rand() &amp; 15) &lt;&lt; 4) +
    (key_t(rand() &amp; 15) &lt;&lt; 8) +
    (key_t(rand() &amp; 15) &lt;&lt; 12) +
    (key_t(rand() &amp; 15) &lt;&lt; 16) +
    (key_t(rand() &amp; 15) &lt;&lt; 20) +
    (key_t(rand() &amp; 15) &lt;&lt; 24) +
    (key_t(rand() &amp; 15) &lt;&lt; 28) +
    (key_t(rand() &amp; 15) &lt;&lt; 32) +
    (key_t(rand() &amp; 15) &lt;&lt; 36) +
    (key_t(rand() &amp; 15) &lt;&lt; 40) +
    (key_t(rand() &amp; 15) &lt;&lt; 44) +
    (key_t(rand() &amp; 15) &lt;&lt; 48) +
    (key_t(rand() &amp; 15) &lt;&lt; 52) + 
    (key_t(rand() &amp; 15) &lt;&lt; 56) +
    (key_t(rand() &amp; 15) &lt;&lt; 60);
}
</code></pre>
<p>Das tun sie aber nicht, die generierten Zufallszahlen sind nicht identisch. Natürlich habe ich den Zufallsgenerator zuvor initialisiert, damit rand() diesselben Zahlen produziert.</p>
<p>Ich vermute, die Reihenfolge bei der Addition in der zweiten Funktion wird vom Compiler verändert. Kann das sein? Oder aus welchen Gründen produzieren die beiden Funktionen unterschiedliche Pseudo-Zufallszahlen?</p>
<p><strong>Danke!</strong></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/254913/reihenfolge-der-addition</link><generator>RSS for Node</generator><lastBuildDate>Sat, 12 Sep 2026 09:20:08 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/254913.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 23 Nov 2009 08:22:58 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Reihenfolge der Addition on Mon, 23 Nov 2009 08:26:47 GMT]]></title><description><![CDATA[<p><strong>Hallo</strong></p>
<p>Ich habe zwei Funktionen geschrieben, die eigentlich exakt denselben Wert zurückgeben sollten:</p>
<pre><code class="language-cpp">key_t board_c::generate_random_key_1(void) {
  key_t result;

  result = 0;

  result += key_t(rand() &amp; 15);
  result += key_t(rand() &amp; 15) &lt;&lt; 4;
  result += key_t(rand() &amp; 15) &lt;&lt; 8;
  result += key_t(rand() &amp; 15) &lt;&lt; 12;
  result += key_t(rand() &amp; 15) &lt;&lt; 16;
  result += key_t(rand() &amp; 15) &lt;&lt; 20;
  result += key_t(rand() &amp; 15) &lt;&lt; 24;
  result += key_t(rand() &amp; 15) &lt;&lt; 28;
  result += key_t(rand() &amp; 15) &lt;&lt; 32;
  result += key_t(rand() &amp; 15) &lt;&lt; 36;
  result += key_t(rand() &amp; 15) &lt;&lt; 40;
  result += key_t(rand() &amp; 15) &lt;&lt; 44;
  result += key_t(rand() &amp; 15) &lt;&lt; 48;
  result += key_t(rand() &amp; 15) &lt;&lt; 52;
  result += key_t(rand() &amp; 15) &lt;&lt; 56;
  result += key_t(rand() &amp; 15) &lt;&lt; 60;

  return result;
}

key_t board_c::generate_random_key_2(void) {
  return
    (key_t(rand() &amp; 15)) +
    (key_t(rand() &amp; 15) &lt;&lt; 4) +
    (key_t(rand() &amp; 15) &lt;&lt; 8) +
    (key_t(rand() &amp; 15) &lt;&lt; 12) +
    (key_t(rand() &amp; 15) &lt;&lt; 16) +
    (key_t(rand() &amp; 15) &lt;&lt; 20) +
    (key_t(rand() &amp; 15) &lt;&lt; 24) +
    (key_t(rand() &amp; 15) &lt;&lt; 28) +
    (key_t(rand() &amp; 15) &lt;&lt; 32) +
    (key_t(rand() &amp; 15) &lt;&lt; 36) +
    (key_t(rand() &amp; 15) &lt;&lt; 40) +
    (key_t(rand() &amp; 15) &lt;&lt; 44) +
    (key_t(rand() &amp; 15) &lt;&lt; 48) +
    (key_t(rand() &amp; 15) &lt;&lt; 52) + 
    (key_t(rand() &amp; 15) &lt;&lt; 56) +
    (key_t(rand() &amp; 15) &lt;&lt; 60);
}
</code></pre>
<p>Das tun sie aber nicht, die generierten Zufallszahlen sind nicht identisch. Natürlich habe ich den Zufallsgenerator zuvor initialisiert, damit rand() diesselben Zahlen produziert.</p>
<p>Ich vermute, die Reihenfolge bei der Addition in der zweiten Funktion wird vom Compiler verändert. Kann das sein? Oder aus welchen Gründen produzieren die beiden Funktionen unterschiedliche Pseudo-Zufallszahlen?</p>
<p><strong>Danke!</strong></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1812122</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1812122</guid><dc:creator><![CDATA[Tomahawk]]></dc:creator><pubDate>Mon, 23 Nov 2009 08:26:47 GMT</pubDate></item><item><title><![CDATA[Reply to Reihenfolge der Addition on Mon, 23 Nov 2009 09:03:34 GMT]]></title><description><![CDATA[<p>Ja in der 2. Variante überlässt Du es dem Compiler in welcher Reihenfolge die einzelnen Ausdrücke ausgewertet werden. + ist im Gegensatz zu ; oder &amp;&amp; und || kein Sequenzpunkt. Ein gutes Beispiel, was passiert wenn man eine Funktion mit Seiteneffekten in einem Ausdruck mehrfach aufruft.</p>
<p>DJohn</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1812130</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1812130</guid><dc:creator><![CDATA[DJohn@work]]></dc:creator><pubDate>Mon, 23 Nov 2009 09:03:34 GMT</pubDate></item><item><title><![CDATA[Reply to Reihenfolge der Addition on Mon, 23 Nov 2009 09:06:22 GMT]]></title><description><![CDATA[<p>In welcher Reihenfolge er addiert ist nicht beobachtbar. Und in welcher Reihenfolge die rand()-Aufrufe gemacht werden, ist vom Standard her nicht definiert. Üblich ist erst hinten dann vorne, also sozusagen genau verkehrt herum.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1812131</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1812131</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Mon, 23 Nov 2009 09:06:22 GMT</pubDate></item><item><title><![CDATA[Reply to Reihenfolge der Addition on Mon, 23 Nov 2009 09:32:56 GMT]]></title><description><![CDATA[<p>Der + Operator ist linksbindend. Also a+b+c ist dasselbe wie (a+b)+c. Allerdings wird bei Dir eine Funktion mit &quot;Seiteneffekten&quot; mehrfach in ein und demselben Ausdruck verwendet. Der C++ Standard definiert aber <strong>nicht</strong>, in welcher Reihenfolge Unterausdrücke berechnet werden.</p>
<p>Kleines Beispiel:</p>
<pre><code class="language-cpp">unsigned u =  (unsigned(rand()) &amp; 15) +
             ((unsigned(rand()) &amp; 15) &lt;&lt; 4);
</code></pre>
<p>Hier ist der dazugehörige Baum:</p>
<pre><code>+
         &amp;                 &lt;&lt;
       /   \          &amp;         4
 unsigned  15       /   \
     |        unsigned  15
   rand()         |
                rand()
</code></pre>
<p>Der Compiler darf jetzt, wenn er will, das rechte rand() zuerst aufrufen.</p>
<p>Gruß,<br />
SP</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1812145</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1812145</guid><dc:creator><![CDATA[Sebastian Pizer]]></dc:creator><pubDate>Mon, 23 Nov 2009 09:32:56 GMT</pubDate></item><item><title><![CDATA[Reply to Reihenfolge der Addition on Mon, 23 Nov 2009 09:34:23 GMT]]></title><description><![CDATA[<p><strong>Danke</strong> <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/1812148</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1812148</guid><dc:creator><![CDATA[Tomahawk]]></dc:creator><pubDate>Mon, 23 Nov 2009 09:34:23 GMT</pubDate></item><item><title><![CDATA[Reply to Reihenfolge der Addition on Mon, 23 Nov 2009 09:48:09 GMT]]></title><description><![CDATA[<p>Und warum wird die Reihenfolge nicht vom Standard definiert? Sind dadurch irgendwelche Geschwindigkeitsoptimierungen möglich?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1812162</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1812162</guid><dc:creator><![CDATA[Danke _)]]></dc:creator><pubDate>Mon, 23 Nov 2009 09:48:09 GMT</pubDate></item><item><title><![CDATA[Reply to Reihenfolge der Addition on Mon, 23 Nov 2009 10:30:15 GMT]]></title><description><![CDATA[<p>Danke _) schrieb:</p>
<blockquote>
<p>Und warum wird die Reihenfolge nicht vom Standard definiert? Sind dadurch irgendwelche Geschwindigkeitsoptimierungen möglich?</p>
</blockquote>
<p>genau das</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1812191</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1812191</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Mon, 23 Nov 2009 10:30:15 GMT</pubDate></item></channel></rss>