Hacker News new | ask | show | jobs
by piersj225 1285 days ago
You can also swap two variables without using a temp variable thanks to XOR

https://stackoverflow.com/a/19618810

2 comments

Unless they are the same memory location!

http://underhanded-c.org/_page_id_16.html

x86 CPUs have a dedicated instruction to swap two registers, or a register with memory.

A note of caution: using that trick on modern hardware and/or languages is likely to be slower than just using the temporary variable. Because compilers and processors can see through the latter better than the XOR.