The nonexistent operator (along = v. == lines)

Checkpoint Technologies ckp at grebyn.com
Wed Apr 3 05:39:36 AEST 1991


In article <156 at revcan.UUCP> darren at 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 at grebyn.com      \\ / /    
Then, the disclaimer:  All expressed opinions are, indeed, opinions. \  / o
Now for the witty part:    I'm pink, therefore, I'm spam!             \/



More information about the Comp.lang.c mailing list