Richard Heathfield said:
CBFalconer said:
Why not?
If you mean that the XOR mechanism is faulty, then whether I agree depends
on what you mean by "faulty". If you simply mean that the mechanism for
updating the Wikipedia is faulty because it means any fool can introduce
random mistakes at any time, then I certainly agree. Nevertheless, santosh
did not say "use the XOR swap" - he merely suggested looking it up. I just
checked the Wikipedia reference he gave and, at the time that I looked, it
seemed to me adequately to cover the various drawbacks of using the XOR
swap. "Look it up" does not mean "look it up and then use it" - it merely
means "look it up".
But it's reasonable to infer that "Look it up" means at least "Look it
up and consider using it". That's certainly how I took it, especially
considering the precondition: "If you really dislike the temporary
variable". If the point is to look it up and then decide, based on
the description, not to use it, then the precondition would be
pointless. I assumed that santosh didn't write point pointless words,
and therefore that he was actually implicitly suggesting that Chad
should consider *using* the xor swap. And even if he wasn't, it seems
very likely that Chad would take it that way.
Just take a look at this text that was present in the Wikipedia entry at
the time I saw it a few moments ago:
"The XOR swap is also complicated in practice by aliasing. As noted above,
if an attempt is made to XOR-swap the contents of some location with
itself, the result is that the location is zeroed out and its value lost.
Therefore, XOR swapping must not be used blindly in a high-level language
if aliasing is possible."
Okay, that text might have changed or vanished by the time you look, but it
seems to me that, in the form stated above, it covers the ground properly.
Not quite. It covers some of the problems, but it doesn't mention the
possibility of trap representations in intermediate results.
Chad: By all means read the Wikipedia article, but don't use the xor
swap trick in your own code.