Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>EDIT: Okay, got it.</p> <p>The first point to make is that obviously you shouldn't use this code anyway. However, when you expand it, it becomes equivalent to:</p> <pre><code>j = j ^ (i = i ^ (j = j ^ i)); </code></pre> <p>(If we were using a more complicated expression such as <code>foo.bar++ ^= i</code>, it would be important that the <code>++</code> was only evaluated once, but here I believe it's simpler.)</p> <p>Now, the order of evaluation of the operands is always left to right, so to start with we get:</p> <pre><code>j = 36 ^ (i = i ^ (j = j ^ i)); </code></pre> <p><strong>This (above) is the most important step.</strong> We've ended up with 36 as the LHS for the XOR operation which is executed last. The LHS is not "the value of <code>j</code> after the RHS has been evaluated".</p> <p>The evaluation of the RHS of the ^ involves the "one level nested" expression, so it becomes:</p> <pre><code>j = 36 ^ (i = 25 ^ (j = j ^ i)); </code></pre> <p>Then looking at the deepest level of nesting, we can substitute both <code>i</code> and <code>j</code>:</p> <pre><code>j = 36 ^ (i = 25 ^ (j = 25 ^ 36)); </code></pre> <p>... which becomes</p> <pre><code>j = 36 ^ (i = 25 ^ (j = 61)); </code></pre> <p>The assignment to <code>j</code> in the RHS occurs first, but the result is then overwritten at the end anyway, so we can ignore that - there are no further evaluations of <code>j</code> before the final assignment:</p> <pre><code>j = 36 ^ (i = 25 ^ 61); </code></pre> <p>This is now equivalent to:</p> <pre><code>i = 25 ^ 61; j = 36 ^ (i = 25 ^ 61); </code></pre> <p>Or:</p> <pre><code>i = 36; j = 36 ^ 36; </code></pre> <p>Which becomes:</p> <pre><code>i = 36; j = 0; </code></pre> <p>I <em>think</em> that's all correct, and it gets to the right answer... apologies to Eric Lippert if some of the details about evaluation order are slightly off :(</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload