News for hackers. Stuff that matters.
Re:Swapping two Numbers without Temporary Variables by paxys on Thursday March 24, 2022 @03:04PM (#30794079)
These aren't algorithms. The language is still doing something under the hood to do the swap.
Re:Swapping two Numbers without Temporary Variables by kzrdude on Thursday March 24, 2022 @03:31PM (#30794351)
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 by lanna on Thursday March 24, 2022 @03:26PM (#30794295)
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 by baseballdork on Thursday March 24, 2022 @03:53PM (#30794598)
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 by abainbridge on Thursday March 24, 2022 @04:32PM (#30795008)
That uses 4 registers and a stack!
Re:Swapping two Numbers without Temporary Variables by Thiez on Thursday March 24, 2022 @06:28PM (#30796185)
You can't do better with a non-inlined function.
Re:Swapping two Numbers without Temporary Variables by happytoexplain on Thursday March 24, 2022 @03:40PM (#30794451)
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 by tialaramex on Friday March 25, 2022 @05:05AM (#30799959)
And on a modern CPU the machine code does not map 1-to-1 to what actually happens when this is executed, the phrase "out-of-order execution" ought to give away that even though your machine code says A, then B, then C, the CPU may decide it was better to do A, then C, then B instead.

Express your actual intent, if your chosen language has a "swap" intrinsic, use it, if not write the swap in a natural and idiomatic way, and let other people do the lifting.

Re:Swapping two Numbers without Temporary Variables by manojlds on Thursday March 24, 2022 @03:16PM (#30794187)
Was that the problem statement? Come up with an algorithm to swap two numbers?
Re:Swapping two Numbers without Temporary Variables by bastawhiz on Thursday March 24, 2022 @08:35PM (#30797430)
Where do you think the intermediate results of doing math on the two variables are kept?