How this logic works is pretty cool. I saw this on the PIClist a few years back.
Here's how it works.
a VAR BYTE
b VAR BYTE
a = %11001100
b = %00110011
a=a^b ' a now = %11001100 ^ %00110011 which = %11111111
1 ^ 0 = 1. 1 ^ 1 = 0. 0 ^ 0 = 0.
b=a^b ' b now = %11111111 ^ %00110011 which = %11001100 (value of original a)
b now contains the original value of a.
a=a^b ' a now = $11111111 ^ %11001100 which = %00110011 (value of orignal b)
Now that b = the original value held in a, ^-oring a with b returns the orignal
value of b, in a.
Using any two values, it still works the same. Like this;
a = %11011100
b = %00110011
a=a^b ' a now = %11011100 ^ %00110011 which = %11101111
b=a^b ' b now = %11101111 ^ %00110011 which = %11011100 (value of original a)
a=a^b ' a now = $11101111 ^ %11011100 which = %00110011 (value of orignal b)
Pretty nifty way of swapping variables.
Bookmarks