Live data from Hacker News

Swapping two Numbers without Temporary Variables

garrit.xyz

11–20 of 93 posts

Re: Swapping two Numbers without Temporary Variables

#12
post #3

I remember just after college I applied for a .net job, when I came in for the interview they gave me a paper exam with a bunch of questions on how to do silly little tricks including that one specifically. I didn't take that job.

I tried to explain to a previous employer how much their little "brainteaser" test was turn off to legitimately talented engineers. I explained that everybody in town knows about the test and basically refuses to even entertain the company because of it.

Bear in mind this is a place building relatively simple web apps... standard line of business stuff. The test was questions like "how many words can you unscramble from these letters" etc.

A majority of the engineers felt the test meant you "knew you were working with smart people". A majority of the code was unmaintainable sludge. (though a few people were extremely talented)

I no longer work there.

Re: Swapping two Numbers without Temporary Variables

#13
post #6
post #5

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

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.

Re: Swapping two Numbers without Temporary Variables

#14
post #6
post #5

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

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

And then the compiled code is doing something under the hood to do whatever the language thought it said.

And then the cpu is doing something under the hood to do whatever the assembly code thought it said.

Re: Swapping two Numbers without Temporary Variables

#16
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.

The downvotes on the OP comment and the negativity in the replies is totally baffling to me. The OP's post was interesting and relevant, despite being non-algorithmic examples. Non-algorithmic examples are not required by the title of the article, and are only implicitly the topic of the article's body, and even if it were explicit, that still wouldn't render the OP comment unrelated. It would have been enough to simply talk about the difference between an algorithmic and language-feature solution without implying something is wrong with even posting the latter.

Re: Swapping two Numbers without Temporary Variables

#17
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...

Re: Swapping two Numbers without Temporary Variables

#18
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?

Re: Swapping two Numbers without Temporary Variables

#19

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...

the second half my comment is wrong. As someone comment points out there is xor swap

Re: Swapping two Numbers without Temporary Variables

#20
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.

Post reply on HN