The nonexistent operator (along = v. == lines)
Richard Harter
rh at smds.UUCP
Fri Apr 5 17:33:52 AEST 1991
In article <157 at revcan.UUCP>, darren at 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.
More information about the Comp.lang.c
mailing list