Code Example
A C function that implements the XOR swap algorithm:
void xorSwap (int *x, int *y) { if (x != y) { *x ^= *y; *y ^= *x; *x ^= *y; } }Note that the code does not swap the integers passed immediately, but first checks if their addresses are distinct. This is because, if the addresses are equal, the algorithm will fold to a triple *x ^= *x resulting in zero.
The body of this function is sometimes seen incorrectly shortened to if (x != y) *x^=*y^=*x^=*y;
. This code has undefined behavior, since it modifies the lvalue *x
twice without an intervening sequence point.
Read more about this topic: XOR Swap Algorithm
Famous quotes containing the word code:
“Many people will say to working mothers, in effect, I dont think you can have it all. The phrase for have it all is code for have your cake and eat it too. What these people really mean is that achievement in the workplace has always come at a priceusually a significant personal price; conversely, women who stayed home with their children were seen as having sacrificed a great deal of their own ambition for their families.”
—Anne C. Weisberg (20th century)
“Wise Draco comes, deep in the midnight roll
Of black artillery; he comes, though late;
In code corroborating Calvins creed
And cynic tyrannies of honest kings;
He comes, nor parlies; and the Town, redeemed,
Gives thanks devout; nor, being thankful, heeds
The grimy slur on the Republics faith implied,
Which holds that Man is naturally good,
Andmoreis Natures Roman, never to be
scourged.”
—Herman Melville (18191891)