I Iove the lesson that you can write a swap function in C, with a temp variable, and gcc with optimization turned on will change the solution to xor.
I had a coworker who had been coding since the 80s at my first job and he showed me this and my young professional mind exploded. It was amazing.
And that’s how we got through the great variable shortage unscathed.
def swap(a, b): a = a XOR b b = a XOR b a = a XOR b
GCC and most compilers will optimize the temp variable to a register. So just using a temp variable instead of explicit XOR is almost always better because of optimization, no extra memory actually gets used. XOR swaps use more cpu cycles.
I both understood this flawlessly and understood non of it at the same time
Similer like this : a = a + b; b = a - b; a = a - b;
The hard part is when a junior developer has to read your code.
Its actually pretty easy if you understand that "a" after the first xor basically becomes a variable that stores which bits are the same and which are different. The purpose of that new "a" is only this for the whole time. So you use this new storage "a" to compare it to the initial "b" to get actually "a" because you knew what is "b" and you knew which bits did it share with the actual "a" using the storage "a" so all you had to do was assign this to the new "b". Then after that you do the same thing but you now use actual "a" which is now the value of the new "b" and compare it to the storage "a" and you get the original "b" that you store in the new "a" or if you want call it the actual new "a" because the first assignment for the "a" was just to have a storage variable
Math seems like magic until you learn it and understand that it actually is magic.
Primeagen : Uses xor JS: Hold my Destructor
This is very similar to swaping variables using operations that are inverse of each other like + and - or * and /. This happens because a xor b xor b = a. So in this xor acts like its own inverse
This is the perfomance nightmare. 6 reads and 3 writes to memory. An optimized compiler can simply do it with register x = load a register y = load b a = register y b = register x 2 reads and 2 writes is all it takes.
(a xor b) xor b = a (a xor b) xor a = b basically, so it's easy to see a' = a xor b b' = a' xor b = (a xor b) xor b = a a'' = a' xor b' = (a xor b) xor b' = (a xor b) xor a = b
Corner case: If a and b are pointers to the same value and the swap is done by: 3x of *a ^= *b; then both a and b will point to 0.
It's easy, just use x86 ASM instruction XCHG, which does swap of values in registers
Basically first XOR works as difference between bits in two numbers. 0 - no difference, 1 - yes difference. Then we just apply it to B. When we apply it we turn B to A initial value. Then to derive the B initial value we apply the same difference to A value which is now stored in B.
If it were efficient to perform xor swap, compilers would use it whenever they encountered a case of a temporary being used to swap two variables. In reality, there's often never a problem with using a scratch register, and in the case of x86, there's an xchg instruction to just swap two registers without a temporary.
Best part is, this is generalizable to Qubits! Perform 3 alternating CNOT(quantum XOR gates) gates and you swap the states of two qubits!
@ThePrimeagen