DEV Community

David Armendáriz
David Armendáriz

Posted on

Swapping two variables with XOR

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

a = a^b
b = b^a
a = a^b
Enter fullscreen mode Exit fullscreen mode

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:

https://youtu.be/F27bOXIW-9M

Top comments (1)

Collapse
 
paddy3118 profile image
Paddy3118

What a lazy interviewer. You should never see this in Python.