SOLUTION: please explain if a=5, b=3; then, a^=b^=a^=b result: a=3, b=5; (Swap) please explain it...

Algebra ->  Test -> SOLUTION: please explain if a=5, b=3; then, a^=b^=a^=b result: a=3, b=5; (Swap) please explain it...      Log On


   



Question 849323: please explain
if a=5,
b=3;
then,
a^=b^=a^=b
result: a=3, b=5; (Swap)
please explain it...

Answer by swincher4391(1107) About Me  (Show Source):
You can put this solution on YOUR website!
Sounds like this is C.
So if we have a =5 and b =3,
we apply an XOR assignment operator.
b = a XOR b
So take 3 XOR 5 for instance. We write each in its 4-bit form
a = 3 XOR 5
(0011)(0101) = (0110) = 6
Now b = 3 a = 6
b = 6 XOR 3
(0110)(0011) = (0101) = 5
b = 5 a = 6
a = 6 XOR 5
(0110)(0101) = (0011) = 3
a = 3 b = 5
To understand what XOR means think of 1 and 0 as true and false. Know the truth table for XOR. It looks like OR but instead TT = F [the exclusive condition] which is why we call it an exclusive or.
Check out this page http://betterexplained.com/articles/swap-two-variables-using-xor/
Let me know if you have any questions!
-Devin