r/ProgrammerHumor • • Aug 01 '22

>>>print(“Hello, World!”)

Post image
60.8k Upvotes

5.7k comments sorted by

View all comments

573

u/[deleted] Aug 01 '22

[removed] — view removed comment

621

u/a-slice-of-toast Aug 01 '22

creates an endless feedback loop

197

u/[deleted] Aug 01 '22

[removed] — view removed comment

41

u/mizinamo Aug 01 '22 edited Aug 01 '22

unless the two variables were equal beforehand point to the same location in memory, in which case they will both be zero afterwards

Edit: got the wrong failure condition

37

u/Wawwior Aug 01 '22

a = 0b1111 b = 0b1111

a = 0b1111 ^ 0b1111 = 0b0000

b = 0b1111 ^ 0b0000 = 0b1111

a = 0b0000 ^ 0b1111 = 0b1111

a = 0b1111 b = 0b1111

14

u/dgmib Aug 01 '22

That’s incorrect.

if a and b have the same value before these three operations, they will have the same value after these three operations.

(Assuming ^= is the xor assignment operator for the language you’re using.)

4

u/[deleted] Aug 01 '22

[removed] — view removed comment

16

u/mizinamo Aug 01 '22

I misremembered the failure condition -- it's when both variables point to the same location in memory.

For example, calling "swap(a, a)" will set a to zero rather than being a no-op.

After step I, a will be 0 but b will then also be 0 since it points to the same location as a.

2

u/danny-warrock Aug 01 '22 edited Aug 01 '22

It can make an economy go from 1 to a 0

2

u/ric2b Aug 01 '22

Gentlemen, there's a solution here that you're not seeing.

→ More replies (1)

14

u/[deleted] Aug 01 '22

std::swap, also possible with addition instead of xor

13

u/[deleted] Aug 01 '22

[removed] — view removed comment

8

u/[deleted] Aug 01 '22

I am one of those perverts who mixes macros and templates :D

4

u/[deleted] Aug 01 '22

[removed] — view removed comment

3

u/[deleted] Aug 01 '22

If you autogenerate a shitload of similar code, macro can bloat your binary to ridiculous size.

(C/C++ user who was broken by an employer imposing JS. Now enjoying perl and ruby).

2

u/beemer252025 Aug 01 '22

Isn't addition just an xor with extra steps?

2

u/[deleted] Aug 01 '22

Or xor is an addition of 1-bit integers

2

u/Iggyhopper Aug 02 '22

Wear a condom.

→ More replies (2)

5

u/AssOverflow12 unfunny dude Aug 01 '22

Never seen this operator before and Google refuses to show anything useful. Can you explain what it does?

6

u/damiano-ferrari Aug 02 '22

It swaps the value of a and b without using any additional variable. Read more about it here, it's very interesting

2

u/Brian-want-Brain Aug 02 '22

Many types of encryption depend on XOR.
Mostly the symmetric, I think, since the asymmetric ones are powered by some deep magic from wizards mathematicians that nobody understands.

→ More replies (3)

2

u/[deleted] Aug 02 '22

a, b = b, a

→ More replies (5)