<=> and obfuscation
Guy Harris
guy at rlgvax.UUCP
Fri Feb 8 15:01:06 AEST 1985
> > To exchange two items without an intermediate variable, try:
> >
> > b ^= a ^= b ^= a
> >
>
> However, if a == b when you start out, you end up with a == 0 && b == 0.
How so? XOR preserves information; i.e., for any two Boolean values "a" and "b"
if you know "a" and "a XOR b" you can always determine "b". Thus, none of
the three XOR steps lose any information. (Try doing it with all four possible
combinations of Boolean values of "a" and "b".)
Of course, none of this changes the fact that 1) as you mention, it only works
if the language permits you to XOR the values, 2) it's obscure, and 3) in at
least 90% of the cases you can do it faster by just moving things around.
Guy Harris
{seismo,ihnp4,allegra}!rlgvax!guy
More information about the Comp.lang.c
mailing list