More generally, if you have functions f, g, and h such that g(f(a, b), b) = a and h(f(a, b), a) = b, then you can do: a <- f(a, b) b <- g(a, b) a <- h(a, b) // Note that b is now the original a In the case of XOR, f, g, and h are all the same. Tom Tom Karzes writes:
If you don't have bitwise XOR, you can do the same thing with addition and subtraction, provided overflow is ignored and you always get the correct low-order bits of the result:
A <- A + B B <- A - B A <- A - B // Note that B is now the original A
Tom
Bernie Cosell writes:
On 14 Mar 2018 at 13:51, rcs@xmission.com wrote:
This reminds me of another famous swapping hack, often used by bit-bumming assembly language hackers (such as myself)..
A -->XOR--> B // That is, set B = A^B
B -->XOR--> A
A -->XOR--> B
There's a related hack those of us used on machines with more limited instruction sets: load a, xor b, and mask, xor b. which loads the bits in 'b' where there are zeroes in the mask. We iused that a lot on the PDP-1d timesharing system.
But most notably, with some addition extended hacking you can use that trick to swap fields between two words. I used that in the IMP code to swap the source IMP address and destination IMP address in setting up the RFNM reply. It was a cute bit of code, because the IMP address was actually 9 bits -- six bits of IMP #, two bits of host and one bit of "fake host" The tricky part is that the fake host bits wasn't contiguous and so it was a cute bit of code to move the fields around all "on the fly", not using an intermediate storage location. I thought it was a beautiful bit of how one could abuse the XOR instruction... nobody else was willing to touch that bit of code... :o)
/Bernie\ Bernie Cosell bernie@fantasyfarm.com -- Too many people; too few sheep --