var a = 7, b = 9;
a = b - a;
b = b - a;
a = a + b;
console.log(a, b); // prints 9 7 var a = 7, b = 9;
a = b - a;
b = b - a;
a = a + b;
console.log(a, b); // prints 9 7 A = A^B
B = A^B
A = A^B
I've seen it used to store prev^next in one pointer in doubly linked lists. The coolest part is traveling forward and backwards depends only on if you use head or the tail to "xor-unpack" the pointer, no different for prev or next! int32_t ab = b ^ a;
int32_t c = b - a;
c ^= ab & (c ^ b);
c >>= 31;
c &= ab;
a ^= c;
b ^= c;
How it works is that if b < a we initially have that c = b - a is negative, and non-negative otherwise, if it were a 33-bit integer. We can then use an arithmetic shift right to spread the top sign bit, creating a mask that is all 1s when b < a and 0s otherwise. However it isn't 33-bit, so we have to detect overflow.If both a and b are non-negative or both negative, there was no overflow. So only if the top bit of both was different (indicating different signs and thus potentially overflow), and the top bit of the result differs from b (since if the signs of a and b differ, the sign of b is our desired result), we flip the top bit of our result. That is the role of the mysterious line c ^= ab & (c ^ b);.
We then as mentioned before do an arithmetic right shift to spread the top bit into the entirety of c, creating a mask. We filter this mask using c &= ab to only contain ones on the positions where a and b differ in bits. Finally we toggle those bit positions by XORing both a and b with the mask, swapping their values if they're out of order, and leaving them unchanged otherwise.
When using a third register, CPUs with register renaming need not even move data at all. They could move the labels “register A”, “register B” around.
a ^= b;
b ^= a;
a ^= b;
Which uses less energy because the bitwise operators do not use the intermediate/interbit carry.In a sort of ironic comedy, maybe an msint.h file could include definitions such as: mint8, sint8, ... mint64, sint64, etc. As well as an explanation about the difference between modular and signed (2s compliment) arithmetic operations.