29
[Meme] Swapping integers in Python
(lemmy.world)
Welcome to the Python community on the programming.dev Lemmy instance!
Past
November 2023
October 2023
July 2023
August 2023
September 2023
I'm gonna need you to elaborate on this one
xor has the property x ^ x = 0, and x ^ 0 = x. So, x ^ x ^ y = y
This is pretty clever, I've never seen this for a swap.
Couldn't have explained it better myself. I don't remember where exactly I first read about this trick, but it's mainly useful in C or Assembly, as you don't have to resort to using a third variable/register.