Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why swapping integer variable by XOR doesn't work in a single line?

Tags:

java

operators

I want to swap the value of two integer variables in java using the XOR operator.

This is my code:

int i = 24;
int j = 17;

i ^= j;
j ^= i;
i ^= j;

System.out.println("i : " + i + "\t j : " + j);

It will work fine but the following equivalent code doesn't work:

int i = 24;
int j = 17;

i ^= j ^= i ^= j;

System.out.println("i : " + i + "\t j : " + j);

Output is like this:

i : 0    j : 24

First variable is zero! What's wrong with Java?

like image 866
Meisam Avatar asked Jul 04 '12 08:07

Meisam


2 Answers

According to Java specification (Java 7 specification), Section 15.26.2 (page 529).

A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T) ((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.

According to Section 15.7 Evaluation Order (Page 423) (emphasis mine):

15.7 Evaluation Order

The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.

15.7.1 Evaluate Left-Hand Operand First

The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated.

If the operator is a compound-assignment operator (§15.26.2), then evaluation of the left-hand operand includes both remembering the variable that the left-hand operand denotes and fetching and saving that variable's value for use in the implied binary operation.

If evaluation of the left-hand operand of a binary operator completes abruptly, no part of the right-hand operand appears to have been evaluated.

Described in more details in Section 15.26.2 (page 529):

If the left-hand operand expression is not an array access expression, then:

• First, the left-hand operand is evaluated to produce a variable. [trimmed]

• Otherwise, the value of the left-hand operand is saved and then the right-hand operand is evaluated. [trimmed]

• Otherwise, the saved value of the left-hand variable and the value of the right-hand operand are used to perform the binary operation indicated by the compound assignment operator. [trimmed]

• Otherwise, the result of the binary operation is converted to the type of the left- hand variable, subjected to value set conversion (§5.1.13) to the appropriate standard value set (not an extended-exponent value set), and the result of the conversion is stored into the variable.

An example in the documentation

Example 15.26.2-2. Value Of Left-Hand Side Of Compound Assignment Is Saved Before Evaluation Of Right-Hand Side

  class Test {
      public static void main(String[] args) {
          int k = 1;
          int[] a = { 1 };
          k += (k = 4) * (k + 2);
          a[0] += (a[0] = 4) * (a[0] + 2);
          System.out.println("k==" + k + " and a[0]==" + a[0]);
      }
  }

So the expression in the question is re-written and grouped as:

i = i ^ (j = j ^ (i = i ^ j));

Left-hand operands are evaluated:

i = 24 ^ (j = 17 ^ (i = 24 ^ 17));
    **

Since the value of i is not "updated" as expected, it will cause the value of i to get 0 when 24 is swapped to j.

like image 166
nhahtdh Avatar answered Nov 02 '22 05:11

nhahtdh


By writing your swap all in one statement, you are relying on side effects of the inner i ^= j expression relative to the outer i ^= (...) expression.

From the Java specificiation (15.26 Assignment Operators):

There are 12 assignment operators; all are syntactically right-associative (they group right-to-left). Thus, a=b=c means a=(b=c), which assigns the value of c to b and then assigns the value of b to a.

[...]

AssignmentOperator: one of = *= /= %= += -= <<= >>= >>>= &= ^= |=

You might want to consider the readability of the code. Perhaps it's best to e.g. put the code in a method called swap(), or do the actual swapping through the use of a temp variable:

int temp = i;
i = j;
j = temp;
like image 27
Andreas Johansson Avatar answered Nov 02 '22 06:11

Andreas Johansson