Path: utzoo!mnetor!uunet!husc6!cmcl2!beta!unm-la!unmvax!nmtsun!hydrovax From: hydrovax@nmtsun.nmt.edu (M. Warner Losh) Newsgroups: comp.lang.c Subject: Re: comma operator Message-ID: <1239@nmtsun.nmt.edu> Date: 22 Jan 88 00:42:05 GMT References: <3819@sigi.Colorado.EDU> <5080013@hpfcdc.HP.COM> <7120@brl-smoke.ARPA> <3887@sigi.Colorado.EDU> Organization: NMT Hydrology program Lines: 27 Summary: It's in lots of books, OSs, etc. 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)) > > Try that, and tell me what you think. A super-genius friend of mine figured > it out. He says it will work for anything (int, char *, etc.). But it > sure boggles my mind... It is a simple mathematical propterty of XOR. That is : a = a xor b b = a xor b a = b xor a swaps the values of A and B. My opsys book had that in it. A systems programming course offered here at NMT has that as one of the home work problems (at least the time I took it). You can find this device in lots of places. The first place that I saw it was in RSTS/E COMMOM.MAC file, where it was used to swap two registers, w/o the need for a temp storage. -- bitnet: lush@nmt.csnet M. Warner Losh csnet: warner%hydrovax@nmtsun uucp: ...{cmcl2, ihnp4}!lanl!unmvax!nmtsun!warner%hydrovax ...{cmcl2, ihnp4}!lanl!unmvax!nmtsun!hydrovax Warning: Hydrovax is both a machine, and an account, so be careful.