Dark Nexus Posted January 13, 2005 Report Posted January 13, 2005 i can remember reading about this in one of my books, but can't remember for the life of me what the heck it actually does. i saw it used to swap variables without using a temporary variable int a = 1, b = 0; a ^= b ^= a ^= b; this sets a to 0 and b to 1 obviously it is overloaded so the original would be a = a ^ b; anyone know?
»SOS Posted January 13, 2005 Report Posted January 13, 2005 Well, it's a funky one A very useful one, too, if you like doing weird and funky stuff (and math/crypto). It's the one and only exclusive or. source ^ modifier = result1001 ^ 1001 = 00001001 ^ 0110 = 11111001 ^ 0101 = 1100 If a source or modifier bit is one (but not both), the resulting bit is one.
Dark Nexus Posted January 13, 2005 Author Report Posted January 13, 2005 well out of the context of 0's and 1's, i use it to swap out characters in a string, it works, and i just don't see how it works. maybe explain in being used to swap out characters in a character array? thanks for the help
»SOS Posted January 16, 2005 Report Posted January 16, 2005 You mean how you can do the thing in the first post?Well, it all comes down to the ones and zeroes, really byte a = 133, b = 44;a ^= b ^= a ^= b; What this does is as follows: Note: a + b does not mean a sum here. It means a sort of a mix. First, a is a and b is b.The first op (a ^= causes a to become a mix of a and b (a is now a + The second op (b ^= a) causes b to become a! What it does is this: it takes a (which is a + and then removes itself ( from that value. So a + b becomes just a.And the last op (a ^= removes the a part (which is now in from a + b (which is in a) and a becomes only b.
Dark Nexus Posted January 17, 2005 Author Report Posted January 17, 2005 ah i see, okies thank you very much
Recommended Posts