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
|
||
NavigationSearchRecent blog postsFeatured ArticleSomething SPECIAL: put variables anywhere18037 | MPC | tipsByte Craft Limited's compilers support SPECIAL memory: user-declared memory blocks accessed (read and written) by software. The compiler manages allocation for SPECIAL memory similarly to system RAM and ROM. The result is a seamless integration of off-chip or on-chip memory resources into C language programs. |
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
|