DEV Community

Discussion on: How can you swap two variables without using a third?

Collapse
 
lexlohr profile image
Alex Lohr

Since xor and subtraction were already mentioned, may I chime in with a circular shift over the 64 bit value of the whole function memory?

Collapse
 
drbearhands profile image
DrBearhands

I love you

Collapse
 
amexboy profile image
Amanu

This one is different, but how exactly would you implement it in a programming language?

Collapse
 
lexlohr profile image
Alex Lohr

Basically, it works like this (x64 assembler):

mov rax, [m64]
ror rax, 32
mov [m64], rax

Thread Thread
 
amexboy profile image
Amanu

No, I mean on a high level programming language which you have no idea where is stored.

Even for an assembly, the two variables could potentially be stored in two different registers. But we're going to assume not!

Cool idea anyways