2 ms·
Reasons not to use XOR swap: 1) It obfuscates (the most obvious thing, but still worth mentioning). 1) It is incorrect if you swap a memory location with itse
by CJefferson 16y ago
Reasons not to use XOR swap:
1) It obfuscates (the most obvious thing, but still worth mentioning).
1) It is incorrect if you swap a memory location with itself (which many algorithms, like sorts, can end up doing without meaning to).
2) It is always slower on any modern optimising compiler than using a temporary in Java, C and C++ (a strong claim to make, but I stand by it. That's why I list specific languages where I know about the internals). Modern compilers use "flow" techniques for temporary variables. A swap will usually compile away to the compiler internally relabelling which value is in which register, and therefore no code at all.