Swapping two variables with XOR

--

XOR truth table

I once got asked in an interview question what the following code does in Python:

a = a^b
b = b^a
a = a^b

I found it weird… It was a multiple choice question and my intuition said that this thing swapped the values of the variables (at least I was not mistaken). So I investigated further and now I explain it in my YouTube channel:

--

--

David Armendáriz
David Armendáriz

Written by David Armendáriz

0 Followers

Mathematician & Software Developer @ eVisit

No responses yet