Xor swap algorithm/C code
Appearance
C Code to implement XOR swap of x and y:
x ^= y; y ^= x; x ^= y;
If you want to use this a lot, it is probably best set up as a compiler macro:
- define xorSwap(x,y) {x=x^y; y=x^y; x=x^y;}
The function declaration would look like:
void xorSwap(int * x, int * y) {
*x = *x ^ *y; *y = *x ^ *y; *x = *x ^ *y;
}
See: Xor swap algorithm