At school someday several years ago I had to do a swap function that swaps two integers, I wanted to do this using bitwise operations without using a third variable, so I came up with this:
void swap( int * a, int * b ) {
*a = *a ^ *b;
*b = *a ^ *b;
*a = *a ^ *b;
}
I thought it was good but when my function was tested by the school's correction program it found an error (of course when I asked they didn't want to tell me), and still today I don't know what didn't work, so I wonder in which case this method wouldn't work.
Do you mind if I ask why? Was there a practical reason for this limitation, or was it just an intellectual puzzle?
I can't be sure what the correction program was complaining about, but one class of inputs this sort of solution is known to fail on is exemplified by
This prints
0, not5.You might say, "Why would anyone swap something with itself?" They probably wouldn't, as I've shown it, but perhaps you can imagine that, in a mediocrely-written sort algorithm, it might end up doing the equivalent of
Now, if it ever happens that
iandjare the same, theswapfunction will wrongly zero outa[i].See also What is the difference between two different swapping function?