I learned using Xor operator to swap two integers,like:
int a = 21;
int b = 7;
a^=b^=a^=b;
I would finally get a=7 and b=21.
I try
You cannot modify a variable more than once without an intervening sequence point, if you do so, it is Undefined Behavior.
a^=b^=a^=b;
Trying to modify the values of a
and b
in the above statement breaks this rule and you end up with an Undefined Behavior.
Note that Undefined Behavior means that any behavior is possible and you can get any output.
Good Reads: