I think the more accepted way for a long time has been to use xor. https://en.wikipedia.org/wiki/XOR_swap_algorithm Its better in a number of ways, and since each bit is independent, it could actually be faster on really low end/old processors. Although these days just about any modern OoO processor will just detect swaps (even if there isn't an actual instruction) and the renamer makes it zero cost.
The only weird thing is that the article has managed to reach the front page. I was under the impression all number (and pointer) swap methods are very well known - with xor being the most applicable. Edit: I am hard pressed to think of an Assembly that doesn't have exchange (swap) between registers - e.g. 6502, but has a xor instead.
Swapping two Numbers without Temporary Variables
41–50 of 93 posts
Re: Swapping two Numbers without Temporary Variables
#42 let a = 9007199254740992;
let b = 1;
a = a + b
b = a - b
a = a - b
a // 1
b // 9007199254740991Re: Swapping two Numbers without Temporary Variables
#43Behold!, as it all becomes the same thing in C++ with optimizations on: https://godbolt.org/z/joh1jhdhq Good production compilers recognize all these swap idioms and will compile them down (i.e., canonicalize them) to the same thing. In the case of things like swapping by adding and xoring, they'll often undo the cleverness and just use temporary registers anyway. These are the kinds of micro-optimizations that compi…
Convert into SSA:
c = a + b;
d = c - b;
e = c - d;
b’ = d;
a’ = e;
Eliminate c: d = a + b - b;
e = a + b - d;
b’ = d;
a’ = e;
Simplify d: d = a;
e = a + b - d;
b’ = d;
a’ = e;
Eliminate dependency of e on d: d = a;
e = a + b - a;
b’ = d;
a’ = e;
Simplify e: d = a;
e = b;
b’ = d;
a’ = e;
Eliminate d: e = b;
b’ = a;
a’ = e;
…resulting in the normal swap.Re: Swapping two Numbers without Temporary Variables
#44Re: Swapping two Numbers without Temporary Variables
#45I think the more accepted way for a long time has been to use xor. https://en.wikipedia.org/wiki/XOR_swap_algorithm Its better in a number of ways, and since each bit is independent, it could actually be faster on really low end/old processors. Although these days just about any modern OoO processor will just detect swaps (even if there isn't an actual instruction) and the renamer makes it zero cost.
Re: Swapping two Numbers without Temporary Variables
#46 let a = 5;
let b = 10;
[a,b] = [b,a];
console.log(a,b) // 10, 5
[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...Re: Swapping two Numbers without Temporary Variables
#47Behold!, as it all becomes the same thing in C++ with optimizations on: https://godbolt.org/z/joh1jhdhq Good production compilers recognize all these swap idioms and will compile them down (i.e., canonicalize them) to the same thing. In the case of things like swapping by adding and xoring, they'll often undo the cleverness and just use temporary registers anyway. These are the kinds of micro-optimizations that compi…
Re: Swapping two Numbers without Temporary Variables
#48Re: Swapping two Numbers without Temporary Variables
#49Re: Swapping two Numbers without Temporary Variables
#50Earlier quoted context omitted.
Indeed, both the algorithm in the article, the rust suggestion, and even the naive solution compile to the same instructions[1]. [1] - https://godbolt.org/z/1P98ss37P
That uses 4 registers and a stack!