Is something terribly wrong with the code?
Yes!
a^=b^=a^=b actually causes Undefined Behavior in C and C ++, because you are trying to change the value of a more than once between two points in a sequence.
Try writing (although not reliable)
a ^= b; b ^= a; a ^= b;
instead of a^=b^=a^=b .
PS : never try to change the values ββof two variables without using a third. Always use the third variable.
EDIT :
As @caf b^=a^=b noted, even if the order of evaluation of the arguments of the ^= operator ^= not specified, since all calls to b in the expression are used to calculate the final value that is stored in b , the behavior is well defined.
Prasoon Saurav Sep 18 '10 at 11:10 2010-09-18 11:10
source share