0% completed
XOR is involute
Miguel
May 6, 2024
I think that it would be helpful to mention that XOR is involute, meaning that it is a self-inverse function. This means that we can apply xor some number of times and undo the operations by applying the XOR function again.
This is why we can effectively "XOR in" all 1->n numbers into x1 and then "XOR in" all numbers in our actual list into x2. Then when we XOR x1 by x2, we are effectively undoing all the numbers we "XOR'ed into" x2 from x1, leaving us only with the number that was never "XOR'ed into" x1 (our missing number).
ex: 0 ^ 1 = 1 ^ 2 = 3 ^ 1 = 2 ^ 2 = 0 (note that reapplying xor got us back to 0)
ex: 0 ^ 1 = 1 ^ 2 = 3 ^ 3 = 0 ^ 4 = 4 ^ 3 = 7 ^ 2 = 5 ^ 1 = 4 (note, we are left with the only number we did not XOR in twice)
1
0
Comments
On this page