Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!cs.utexas.edu!rice!uupsi!grebyn!ckp From: ckp@grebyn.com (Checkpoint Technologies) Newsgroups: comp.lang.c Subject: Re: The nonexistent operator (along = v. == lines) Keywords: xor Message-ID: <1991Apr2.193936.5774@grebyn.com> Date: 2 Apr 91 19:39:36 GMT References: <156@revcan.UUCP> Organization: Grebyn Timesharing Lines: 30 In article <156@revcan.UUCP> darren@revcan.UUCP (Darren Morbey) writes: >I've noticed in my writing C code that there is no such operator >as ^^ (which would be to ^ as || is to |). I feel in this case >I *have* to write a macro for this *nonexistent* operator (you may >recall I wrote #define XOR(a,b) ((a)^(b)) ). However... > >I would like to use one of the following three macros, but each has >its own particular problems. I would appreciate any advice on which >one should be used. I would like it to operate like && and ||. An ^^ operator cannot operate just like && and ||. This is because there is no possible notion of a shortcut evaluation; both sides must be evaluated in every case. I'll assume you mean for ^^ to operate on operands which are only treated as 0 or non-0, and returns either 0 or 1. >1. #define XOR(a,b) ( ( !(a) && (b) ) || ( (a) && !(b) ) ) >2. #define XOR(a,b) ( !(a) != !(b) ) >3. #define XOR(a,b) ( (a) ? !(b) : (b) ) /* my favourite. */ Here's mine: #define XOR(a,b) (((a) != 0) ^ ((b) != 0)) -- First comes the logo: C H E C K P O I N T T E C H N O L O G I E S / / ckp@grebyn.com \\ / / Then, the disclaimer: All expressed opinions are, indeed, opinions. \ / o Now for the witty part: I'm pink, therefore, I'm spam! \/