DEV Community

David Armendáriz
David Armendáriz

Posted on

2 2

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.

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more