The following code swaps values between 2 variables without using a temp variable.....
i want to know the logic behind this

//#include "stdafx.h"
#include<stdio.h>
int main(){
int a =3, b=5;
a^=b^=a^=b;
printf("%d" "\n" "%d",a,b);
return 0;
}
Output
5
3

>The following code swaps values between 2 variables without using a temp variable.....
Actually, it invokes undefined behavior and could really do anything. If you add sequence points between the operations, then the code is technically correct:

a ^= b;
b ^= a;
a ^= b;

Figure out the bit value for a and b, then work it out yourself using the truth table for XOR:

INPUT 	OUTPUT
A 	B 	A XOR B
0 	0 	0
0 	1 	1
1 	0 	1
1 	1 	0

So let's say a == 1010 and b == 0101:

1010 XOR 0101 = 1111 (a = 1111)
0101 XOR 1111 = 1010 (b = 1010)
1111 XOR 1010 = 0101 (a = 0101)

End result, a == 0101 and b == 1010, which is an inversion of the original state. The XOR swap is an archaic trick to avoid the need for a temporary variable. It's still relevant if you're writing assembly or on a severely memory constrained platform, but for the most part not recommended because

  1. As you've proven, it's not as obvious what's going on.
  2. Tricks like this can actually hinder a compiler's optimizer, resulting in less efficient machine code.
  3. It doesn't always work. For example, if you XOR an object with itself, the result is 0 rather than the expected unchanged value. When a common implementation uses pointers, it's not an unexpected situation and needs a special case (which further complicates and slows things down):
    template <typename T>
    void swap(T& a, T& b)
    {
        if (&a != &b) {
            a ^= b;
            b ^= a;
            a ^= b;
        }
    }

Edited 6 Years Ago by Narue: n/a

Oh I thought it was "A is to power B" thus I was surprised with the both expression and results. Thanks Narue to show it properly.

>The following code swaps values between 2 variables without using a temp variable.....
Actually, it invokes undefined behavior and could really do anything. If you add sequence points between the operations, then the code is technically correct:

a ^= b;
b ^= a;
a ^= b;

Figure out the bit value for a and b, then work it out yourself using the truth table for XOR:

INPUT 	OUTPUT
A 	B 	A XOR B
0 	0 	0
0 	1 	1
1 	0 	1
1 	1 	0

So let's say a == 1010 and b == 0101:

1010 XOR 0101 = 1111 (a = 1111)
0101 XOR 1111 = 1010 (b = 1010)
1111 XOR 1010 = 0101 (a = 0101)

End result, a == 0101 and b == 1010, which is an inversion of the original state. The XOR swap is an archaic trick to avoid the need for a temporary variable. It's still relevant if you're writing assembly or on a severely memory constrained platform, but for the most part not recommended because

  1. As you've proven, it's not as obvious what's going on.
  2. Tricks like this can actually hinder a compiler's optimizer, resulting in less efficient machine code.
  3. It doesn't always work. For example, if you XOR an object with itself, the result is 0 rather than the expected unchanged value. When a common implementation uses pointers, it's not an unexpected situation and needs a special case (which further complicates and slows things down):
    template <typename T>
    void swap(T& a, T& b)
    {
        if (&a != &b) {
            a ^= b;
            b ^= a;
            a ^= b;
        }
    }

thank you very much Narue for the detailed explanation

This question has already been answered. Start a new discussion instead.