Encyclopedia > Xor swap algorithm C code

  Article Content

Xor swap algorithm/C code

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:

  1. 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



All Wikipedia text is available under the terms of the GNU Free Documentation License

 
  Search Encyclopedia

Search over one million articles, find something about almost anything!
 
 
  
  Featured Article
Battle Creek, Michigan

... for a household in the city is $35,491, and the median income for a family is $43,564. Males have a median income of $36,838 versus $26,429 for females. The per capita ...

 
 
 
This page was created in 129.8 ms