Path: utzoo!attcan!uunet!cs.utexas.edu!tut.cis.ohio-state.edu!mailrus!ames!uhccux!munnari.oz.au!mimir!hugin!augean!sirius!nt!levels!ccdn From: ccdn@levels.sait.edu.au (DAVID NEWALL) Newsgroups: comp.lang.c Subject: Re: Obfuscated SWAP Message-ID: <1267@levels.sait.edu.au> Date: 25 Aug 89 10:55:06 GMT References: <784@skye.ed.ac.uk> Followup-To: comp.lang.c Organization: Sth Australian Inst of Technology Lines: 32 In article <784@skye.ed.ac.uk>, ken@aiai.ed.ac.uk (Ken Johnson) writes: > Well, I tried it and it works > > x ^= y ^= x ^= y; /* Swap X and Y over */ > > I don't understand how it works The intent of this code is clear. It is intended to work like this: Given the variables x and y, which have initial values a and b respectively. Then: x ^= y ^= x ^= y | | | | | | | x = a, y = b | | | | | x = a ^ b, y = b | | | y = b ^ (a ^ b) = (b ^ b) ^ a = a, x = a ^ b | => y = a, x = a ^ b | x = (a ^ b) ^ a = (a ^ a) ^ b = b, y = a => x = b, y = a (You realise, of course, that ^ is the exclusive or operator) David Newall Phone: +61 8 343 3160 Unix Systems Programmer Fax: +61 8 349 6939 Academic Computing Service E-mail: ccdn@levels.sait.oz.au SA Institute of Technology Post: The Levels, South Australia, 5095