Path: utzoo!utgpu!cunews!micor!latour!revcan!darren From: darren@revcan.UUCP (Darren Morbey) Newsgroups: comp.lang.c Subject: The nonexistent operator (along = v. == lines) Keywords: xor Message-ID: <156@revcan.UUCP> Date: 2 Apr 91 13:59:47 GMT Organization: Revenue Canada, Taxation; Office Communications Division Lines: 39 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... The Pascal xor operator is <>, which follows since it has a Boolean type on which its and & or operators can (and only will) operate. Since C uses any non-zero value as 'true', it seems the only reason to include && and || to avoid trouble with & and |. The problem is, there is no ^^ to avoid trouble with ^. 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 ||. 1. #define XOR(a,b) ( ( !(a) && (b) ) || ( (a) && !(b) ) ) - Althought this is straight from the definition of XOR ( as (NOT A AND B) OR (A AND NOT B) ) it must evaluate a and b *twice* (obvious loss of flexibility) 2. #define XOR(a,b) ( !(a) != !(b) ) - This is meant to overcome C's acceptance of multiple TRUEs ( unlike Pascal's single TRUE value ). This relies on the following statements being true: ( 0 == 0L ) (since a and b may yield different sized int's) ( !0 == !(0L) ) ( !(0L) == !('\0') ) (which may happen!) etc. Can such expressions be *guaranteed* true? 3. #define XOR(a,b) ( (a) ? !(b) : (b) ) /* my favourite. */ - This relies simply on one thing: in the conditional expression, do *both* !(b) and (b) get evaluated, regardless of which is returned? Or is only the value to be returned evaluated. (Final note: This *is* meant to be *portable*. Be very careful.) [ Darren Morby +1 613 957 9281 {uunet|lsuc|fts1}!revcan!darren ] [ Revenue Canada Taxation, 3052-400 Cumberland St, Ottawa,ONT K1A 0L8 ] (included for those who wish to send mail, call me, or send any lynch) (mobs you can hire. :-) )