num1 = 5;
num2 = 10;
void SwapUsingXOR(int &num1, int &num2)
{
num1 = num1 ^ num2;
num2 = num1 ^ num2;
num1 = num1 ^ num2;
}
So, my realization of this algorithm is
-
When i do the 1st step when we XOR the values its takes both values input in the form of 0 and 1 and takes advantage of binary’s compliment attitude i.e. !0 => 1 or !1 => 0
-
On the 2nd step when we XOR the first second value with the previously recorded value, we get the other (1st) value. i.e. 5
-
On the 3rd step when we XOR the updated 2nd value with the previously value recorded, we get the another (2nd) value. i.e. 10
My observation is right? or I’m totally wrong. If I’m wrong can you tell me why?
I’m posting this to know others thought on this.