AlgoScope

XOR Properties

a ^ a = 0, a ^ 0 = a, commutative and associative. Pairs cancel.

Bit manipulation, mathematics, beginner
b becomes 6, which is the original a.15061
b becomes 6, which is the original a.15061

XOR Tricks: Swap without a temporary

Decision, step 3 of 5

b = a xor b = 15 xor 9 = 6. That is the original a: (6 xor 9) xor 9 leaves 6, because the 9 cancels itself.

How xor tricks works

Cost

Space
O(1)

Leads to

Topics that need this one first.

Taught by the same lesson

XOR Tricks covers these too, in the same run.