Neat XOR trick
This is a neat trick using XOR, the most mysterious of the logical operators. Best of all for embedded, it requires no temporary locations.
//swap two values in place
if(x < y)
{
x = x ^ y;
y = y ^ x;
x = x ^ y;
}
Et voila!
Byte Craft LimitedCode Development Systems
|
||
NavigationSearch |
Neat XOR trick
Submitted by Kirk Zurell on Mon, 2007-09-24 13:20.
tips
This is a neat trick using XOR, the most mysterious of the logical operators. Best of all for embedded, it requires no temporary locations.
//swap two values in place
if(x < y)
{
x = x ^ y;
y = y ^ x;
x = x ^ y;
}
Et voila! |
DownloadsLooking for demos or updated software? See the Byte Craft Limited Depot
|