this post was submitted on 19 Jun 2023
44 points (87.9% liked)

Python

6420 readers
30 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
44
submitted 1 year ago* (last edited 1 year ago) by jnovinger to c/python
 

Yay or nay, memes should be allowed in [email protected] ?

you are viewing a single comment's thread
view the rest of the comments
[–] Hammerheart 1 points 1 year ago (1 children)

I'm gonna need you to elaborate on this one

[–] dmad 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.

[–] [email protected] 2 points 1 year 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.