Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site gatech.UUCP Path: utzoo!watmath!clyde!burl!ulysses!gatech!arnold From: arnold@gatech.UUCP (Arnold Robbins) Newsgroups: net.lang.c Subject: using ^= to swap two integers Message-ID: <11950@gatech.UUCP> Date: Fri, 8-Feb-85 16:18:56 EST Article-I.D.: gatech.11950 Posted: Fri Feb 8 16:18:56 1985 Date-Received: Sat, 9-Feb-85 07:46:58 EST Distribution: net Organization: Firefighters Extraordinaire, Inc. Lines: 32 I blew it: >> Jon Shapiro > me >> To exchange two items without an intermediate variable, try: >> >> b ^= a ^= b ^= a >> >> Jon Shapiro > >However, if a == b when you start out, you end up with a == 0 && b == 0. I tried it, and in fact it does end up swapping them, but since they were equal in the first place, it is a null op. Several people have pointed out to me that I was wrong, so please, no more mail. I goofed. (Here is the program which proved me wrong.) main() { int a = 3, b = 3; b ^= a ^= b ^= a; printf ("a = %d\tb = %d\n", a, b); } -- Arnold Robbins CSNET: arnold@gatech ARPA: arnold%gatech.csnet@csnet-relay.arpa UUCP: { akgua, allegra, hplabs, ihnp4, seismo, ut-sally }!gatech!arnold Help advance the state of Computer Science: Nuke a PR1ME today!