The swap with bitwise operators will be the fastest at the hardware level. :)
Yes. Whenever you can achieve something using bitwise operations I always prefer that.
Rather disappointingly, in most modern hardware it's the one with the temp variable that is fastest.
Depending on the architecture and compiler, they end up using three registers to do a swap under the hood I think.
Yes, but the idea is that you do this in assembly. It was a common trick because it saved a register and (at worst) it ran in the same number of cycles.
When I started going through the comments, I was actually thinking "what about bitwise xor?" And sure enough, here it is. ^,^
^,^
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
The swap with bitwise operators will be the fastest at the hardware level. :)
Yes. Whenever you can achieve something using bitwise operations I always prefer that.
Rather disappointingly, in most modern hardware it's the one with the temp variable that is fastest.
Depending on the architecture and compiler, they end up using three registers to do a swap under the hood I think.
Yes, but the idea is that you do this in assembly. It was a common trick because it saved a register and (at worst) it ran in the same number of cycles.
When I started going through the comments, I was actually thinking "what about bitwise xor?" And sure enough, here it is.
^,^