Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!mips!ultra!jerbil From: jerbil@ultra.com (Joseph Beckenbach {Adapter Software Release Engr}) Newsgroups: comp.lang.c Subject: Portable XOR Message-ID: <1991Apr18.161333.3463@ultra.com> Date: 18 Apr 91 16:13:33 GMT References: <156@revcan.UUCP> <157@revcan.UUCP> Organization: Ultra Network Technologies Lines: 43 In <157@revcan.UUCP> darren@revcan.UUCP (Darren Morbey) writes, asking for a macro returning XOR of its arguments, evaluating once and only once. I've not worked out a more elegant solution for my toolbag, alas: ------ #include #define XOR( a, b ) \ ( t1=((a)!=0), t2=((b)!=0), ( (t1 && t2) ? 0 : (t1 || t2) ) ) int main( ) { int t1, t2; printf( "0 0 %d\n", XOR(0,0) ); printf( "0 1 %d\n", XOR(0,1) ); printf( "1 0 %d\n", XOR(1,0) ); printf( "1 1 %d\n", XOR(1,1) ); } ------ Output: 0 0 0 0 1 1 1 0 1 1 1 0 The variables t1 and t2 need to be in-scope when XOR is used -- I usually place these as file-scope static globals (documented, of course). It uses the standard convention of representing boolean "false" as 0, "true" as non-0. Note that this takes pointers as well, and will interpret (type*)NULL as "false". Why this might be useful, I don't know.... Joseph Beckenbach journeyman programmer -- Joseph Beckenbach jerbil@ultra.com VEGGIES FOREVER! (aka Joseph d'Aquitaine, aka the Stainless Steel Jerbil) work 408-922-0100 x246 home 408-983-2944