Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!snorkelwacker.mit.edu!bloom-beacon!bu.edu!transfer!lectroid!jjmhome!smds!rh From: rh@smds.UUCP (Richard Harter) Newsgroups: comp.lang.c Subject: Re: The nonexistent operator (along = v. == lines) Summary: This may be what you want. Keywords: xor Message-ID: <372@smds.UUCP> Date: 5 Apr 91 07:33:52 GMT References: <156@revcan.UUCP> <157@revcan.UUCP> Organization: SMDS Inc., Concord, MA Lines: 29 In article <157@revcan.UUCP>, darren@revcan.UUCP (Darren Morbey) writes: > What I did require was an operator, macro, or function that treated > its operands as "zero" or "non-zero" as && and || do rather than > the bitwise & | ^. I also would like some guarantee that both > operands were evaluated *once* *and* *only* *once* (O&OO)... Then I believe that what you want is #define XOR(a,b) ( !(a) ? (!!(b)) : (!(b)) ) which returns 1 if exactly one of the two arguments is 0 and 0 otherwise, with both arguments being evaluated exactly once. As far as I can see this is the only way to meet your requirements. I am not at all sure if it is possible to write macros for AND and OR that meet your requirements. The problem I see is this: #define AND(a,b) ( !(a) ? ((b) && 0) : !!(b) ) appears to do the trick, modulo typoes. However it occurs to me that a "clever" optimizing compiler would recognize that ((b) && 0 ) is always false and bypass the evaluation of b. Perhaps the language lawyers can tell us if the language specifications *require* that b be evaluated. -- Richard Harter, Software Maintenance and Development Systems, Inc. Net address: jjmhome!smds!rh Phone: 508-369-7398 US Mail: SMDS Inc., PO Box 555, Concord MA 01742 This sentence no verb. This sentence short. This signature done.