29
submitted 3 years ago* (last edited 3 years ago) by jnovinger@programming.dev to c/python@programming.dev

Yay or nay, memes should be allowed in !python@programming.dev ?

you are viewing a single comment's thread
view the rest of the comments
[-] dmad@programming.dev 5 points 3 years ago* (last edited 3 years ago)

xor has the property x ^ x = 0, and x ^ 0 = x. So, x ^ x ^ y = y

a' = a ^ b
b' = b ^ a' = b ^ (a ^ b) = a
a'' = (a ^ b) ^ a = b

This is pretty clever, I've never seen this for a swap.

[-] ActuallyRuben@actuallyruben.nl 2 points 3 years ago

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.

this post was submitted on 19 Jun 2023
29 points (87.2% liked)

Python

8028 readers
6 users here now

Welcome to the Python community on the programming.dev Lemmy instance!

📅 Events

PastNovember 2023

October 2023

July 2023

August 2023

September 2023

🐍 Python project:
💓 Python Community:
✨ Python Ecosystem:
🌌 Fediverse
Communities
Projects
Feeds

founded 3 years ago
MODERATORS