View Single Post
Old 01-19-2004, 07:57 PM   #2 (permalink)
HFrankenstein
Crazy
 
Location: New Jersey / Delaware
Here goes...

To make it easier to look at, stick some parens in there (though I believe it will produce a syntax error) [edit]VC++ doesn't seem to mind.[/edit]

x ^= (y ^= (x ^= y))

The innermost statement (x ^= y) will set x = 100(bin) = 4(dec)

Then, the middlemost(?) statement (y ^= (x ^= y)) sets y = (101 ^ 100) = 1

Finally, the outermost statement x ^= (y ^= (x ^= y)) sets x = (100 ^ 001) = 101(bin) = 5(dec)

In effect, it switches x and y. I could show the algebra that proves that this works for any x and y, but I'm lazy, so bleh.
__________________
When in doubt, sauerkraut.
HFrankenstein is offline  
 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47