Path: utzoo!utgpu!water!watmath!clyde!rutgers!rochester!ritcv!cci632!ccicpg!puivax!ian From: ian@puivax.UUCP (Ian Wilson) Newsgroups: comp.lang.c Subject: Re: comma operator (really about xor) Message-ID: <131@puivax.UUCP> Date: 27 Jan 88 21:45:08 GMT References: <3819@sigi.Colorado.EDU> <5080013@hpfcdc.HP.COM> <7120@brl-smoke.ARPA> <3476@ihlpf.ATT.COM> <564@cresswell.quintus.UUCP> Reply-To: ian@puivax.UUCP (Ian Wilson) Organization: Philips Ultrasound International, Santa Ana, CA. Lines: 31 In article <564@cresswell.quintus.UUCP> ok@quintus.UUCP (Richard A. O'Keefe) 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)) > > swap(a, a); >sets a to 0, whatever its previous value was. If you do > swap(*p, *q); >it can easily happen that p==q, in which case you get this problem. I am posting this because I once had a *useful* application of this way of swapping two pieces of storage without using any temporary storage. Using an image processor with an asymmetric ALU, ie. it expected operand A to come from image memory A and operand B from B, it enabled me to swap memories in 3 operations: 1. A ^= B 2. B ^= A 3. A ^= B (At least the ALU knew how to xor!!). Each image memory was 1/4Mb, so doing it this way (90ms) was OK, compared with the prospect of moving 2x1/4M off to mass storage and back again..... The transformation works whatever the values of A & B happen to be - exclusive or being the only information-preserving logical operation. So the original `swap' macro may or may not be very useful but the principle is correct. ---- ccicpg!puivax!ian