Path: utzoo!utgpu!water!watmath!clyde!cbosgd!ihnp4!ihlpf!nevin1 From: nevin1@ihlpf.ATT.COM (00704A-Liber) Newsgroups: comp.lang.c Subject: Re: comma operator Message-ID: <3476@ihlpf.ATT.COM> Date: 22 Jan 88 01:57:29 GMT References: <3819@sigi.Colorado.EDU> <5080013@hpfcdc.HP.COM> <7120@brl-smoke.ARPA> <7137@brl-smoke.ARPA> <2382@ihuxv.ATT.COM> Reply-To: nevin1@ihlpf.UUCP (00704A-Liber,N.) Organization: AT&T Bell Laboratories - Naperville, Illinois Lines: 34 In article <2382@ihuxv.ATT.COM> rck@ihuxv.ATT.COM (R. C. Kukuk) writes: >> In article <3887@sigi.Colorado.EDU> swarbric@tramp.Colorado.EDU (SWARBRICK FRANCIS JOHN) writes: >> >#define swap(a,b) ((a) = ((b) = ((a) = (a) ^ (b)) ^ (b)) ^ (a)) >> >He says it will work for anything (int, char *, etc.). > >This oldie fails when a == b. Why? Think about it. I did. I still don't see why it fails. The macro is equivalent to (assuming a and b are either vars or constants of the same type and that type allows the ^ operator) the following evaluated in the order presented: a = a ^ b b = a ^ b a = a ^ b Here is an example where a == b (numbers given in binary) Let a = b = 00101101 swap(a,b): a = a ^ b === 00101100 ^ 00101101 === 00000000 b = a ^ b === 00000000 ^ 00101101 === 00101101 a = a ^ b === 00000000 ^ 00101101 === 00101101 Hence, a = b = 00101101. QED -- _ __ NEVIN J. LIBER ..!ihnp4!ihlpf!nevin1 (312) 510-6194 ' ) ) "The secret compartment of my ring I fill / / _ , __o ____ with an Underdog super-energy pill." / (_