Switch two variables without using a temporary stand-in
Wednesday, February 28, 2007 10:16:00 PM
I remember "learning" this trick 6 years ago, but since I never understood how it worked I quickly forgot it again. Since I stumbled upon it again, I decided to have a look at why it worked so I'd remember it in case I ever get a use for it - like how I know that a second, since World War II, has been defined as the time it takes the cesium 133 atom to oscillate 9 192 631 770 times. (And now this is obsolete for some other atom with a number so much larger it's not even funny, but sad, to remember).
The trick is to use bitwise XOR on two variables in a specific pattern so they switch their values such that A contains what B had, and vice versa. This isn't actually terribly useful for most programmers to know since memory restrictions aren't exactly quite what they used to be, but as an amusing puzzle it's worth mentioning.
var a = 5; var b = 3; a = a^b; b = a^b; a = a^b; alert('a is '+a+' and b is '+b);
To test it, copy the following into your address bar and hit <enter>:
The output should tell you that a is 3 and b is 5, even though we initialized a as 5 and b as 3. The reason it works is that all four possible combinations of bit patterns are switched to their opposite by performing the same XOR operations, as we shall see:
|Initial values||1st pass||2nd pass||3rd pass|
|0 0||0 0||0 0||0 0|
|0 1||1 1||1 0||1 0|
|1 1||0 1||0 1||1 1|
|1 0||1 0||1 1||0 1|
In my eyes, the beauty in this is that there are no if-else statements or special conditions. Everything works by a simple bitwise operator, making it very fast and memory preserving to boot! ...albeit very little memory. Absolutely everything that can be expressed as a bitstream can theoretically be switched in this fashion, though language restrictions usually prevent you from doing this on strings.