Potential Problem in "Swapping values of two variables without using a third variable"

Viewed 5114

I recently came along this method for swapping the values of two variables without using a third variable.

a^=b^=a^=b

But when I tried the above code on different compilers, I got different results, some gave correct results, some didn't.

Is anything terribly wrong with the code?

8 Answers

What about this one?

a = a + b;
b = a - b;
a = a - b;
Related