3 ms·
You can also swap two variables without using a temp variable thanks to XOR https://stackoverflow.com/a/19618810 https://stackoverflow.com/a/19618810
by piersj225 4y ago
You can also swap two variables without using a temp variable thanks to XOR
https://stackoverflow.com/a/19618810 https://stackoverflow.com/a/19618810
- rep_lodsb 4y agoUnless they are the same memory location! http://underhanded-c.org/_page_id_16.html http://underhanded-c.org/_page_id_16.html x86 CPUs have a dedicated instruction to swap two registers, or a register with memory.
- eru 4y agoA 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.