Live data from Hacker News

Swapping two Numbers without Temporary Variables

garrit.xyz

21–30 of 93 posts

Re: Swapping two Numbers without Temporary Variables

#21

To deal with overflow the size of variable a needs to be doubled. This trick, if applied correctly, uses the same amounts of memory. Now you may wonder whether we can mathematically prove swapping two variavles requires memory of three variables in digital computer...

You could use unsigned ints, whose arithmetic wraps, to deal with the overflow.

I'd still be curious about any theories of storage.

Re: Swapping two Numbers without Temporary Variables

#22
post #13
post #6

Earlier quoted context omitted.

These aren't algorithms. The language is still doing something under the hood to do the swap.

Some people seem to forget that in high-level programming languages, lines of code seldom map 1-to-1 to processor instructions.

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

Re: Swapping two Numbers without Temporary Variables

#23
post #15

The person that developed this trick is obviously familiar with the Mathematical properties of numbers. But this is the wrong approach from many prospective, including the impact to performance, the potential of under/overflow, and the lack of readability.

The author explicitly states that it should never be used due to readability. I'm interested in your point about performance, though - as a layman in this area, I would have thought that the additional operations were cheaper than whatever the overhead of a variable is, even for a primitive value type. Is that definitely not the case?

In most cases swapping two variables with a temporary variable will be either zero-overhead -- because you've just changed the names you're referring to those variables by, and the compiler knows it -- or very, very low-overhead. You're not actually saving any memory with the fancy tricks.

Re: Swapping two Numbers without Temporary Variables

#24
post #5

Or, in Python a, b = b, a Or Rust: (a, b) = (b, a);

For what it's worth, the Python example isn't too relevant since it's hiding a number of intermediate variables (that said, swapping without intermediate variables tends to be more of a curiosity anyway -- and a motivated person can "well actually" the whole concept into oblivion). What it does is push a and b onto the interpreter stack, then (in the version of Python 3 I'm running, though the more recent one should be similar) it pops those into two local C variables and pushes them in a different order, then it pops them and stores them into local python variables.

  import dis
  
  def swap(a, b):
      a, b = b, a
      return a
  
  dis.dis(swap)
Output:

  4           0 LOAD_FAST                1 (b)
              2 LOAD_FAST                0 (a)
              4 ROT_TWO
              6 STORE_FAST               0 (a)
              8 STORE_FAST               1 (b)

  5          10 LOAD_FAST                0 (a)
             12 RETURN_VALUE
Implementation of ROT_TWO https://github.com/python/cpython/blob/bc85eb7a4f16e9e2b6fb7...

Re: Swapping two Numbers without Temporary Variables

#25

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.

[deleted]

Re: Swapping two Numbers without Temporary Variables

#27

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.

Re: Swapping two Numbers without Temporary Variables

#28

To deal with overflow the size of variable a needs to be doubled. This trick, if applied correctly, uses the same amounts of memory. Now you may wonder whether we can mathematically prove swapping two variavles requires memory of three variables in digital computer...

You could use unsigned ints, whose arithmetic wraps, to deal with the overflow. I'd still be curious about any theories of storage.

Javacript doesn't have an unsigned int datatype. The numbers in the article were doubles.

Re: Swapping two Numbers without Temporary Variables

#29
post #27

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.

https://xkcd.com/1053/

Re: Swapping two Numbers without Temporary Variables

#30
post #27

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.

Indeed, I first learned about xor-swap from an example in the IBM System/360 Principles of Operations manual, circa 1968. (For some funky reason I don't understand, the Programming Examples section vanished from later editions of PrincOps.) I can't see any reason for wanting to use arithmetic instead.
Post reply on HN