@ThePrimeagen

Real World Application: ULPFEC -- https://datatracker.ietf.org/doc/html/rfc5109

I had to create tests around this at my job for Real Time Gaming at netflix.  Forward error correction is really important in real time communications!

@ConsoleKits

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.

@erikberg2893

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.

@ItsDan123

And that’s how we got through the great variable shortage unscathed.

@DrakePitts

def swap(a, b):
    a = a XOR b
    b = a XOR b
    a = a XOR b

@TheChrisSimpson

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.

@askerafaunov5424

I both understood this flawlessly and understood non of it at the same time

@Rafi-Riday

Similer like this :
a = a + b;
b = a - b;
a = a - b;

@surinassawajaroenkoon6064

The hard part is when a junior developer has to read your code.

@Oguzhan-n5f

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

@user-kt0jl90sfwj8cb

Math seems like magic until you learn it and understand that it actually is magic.

@Abdullah-zd5rz

Primeagen : Uses xor
JS: Hold my Destructor

@ananthshetty2746

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

@igorfujs7349

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.

@puncherinokripperino2500

(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

@AntonErholt

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.

@playday3008

It's easy, just use x86 ASM instruction XCHG, which does swap of values in registers

@dmitrirodzik

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.

@KiranasOfRizon

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.

@Anonypersona

Best part is, this is generalizable to Qubits! Perform 3 alternating CNOT(quantum XOR gates) gates and you swap the states of two qubits!