Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site rlgvax.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!godot!harvard!seismo!rlgvax!guy From: guy@rlgvax.UUCP (Guy Harris) Newsgroups: net.lang.c Subject: Re: <=> and obfuscation Message-ID: <441@rlgvax.UUCP> Date: Fri, 8-Feb-85 00:01:06 EST Article-I.D.: rlgvax.441 Posted: Fri Feb 8 00:01:06 1985 Date-Received: Sat, 9-Feb-85 08:58:39 EST References: <7699@brl-tgr.ARPA> <1293@bbncca.ARPA> <7853@brl-tgr.ARPA> <805@sjuvax.UUCP> <11927@gatech.UUCP> Organization: CCI Office Systems Group, Reston, VA Lines: 18 > > 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