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

Python

6360 readers
12 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 1 year ago
MODERATORS
 

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
[–] ActuallyRuben@actuallyruben.nl 3 points 1 year ago (3 children)
[–] Hammerheart@programming.dev 1 points 1 year ago (2 children)

I'm gonna need you to elaborate on this one

[–] dmad@programming.dev 5 points 1 year ago* (last edited 1 year ago) (1 children)

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.

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.