[comp.lang.c] conditional expression evaluati

stephen@datacube.UUCP (01/19/87)

> My small contribution to this silly topic:
> 
>     x = (*cp++ | *cp++ | *cp++);                                (1)
>     if (x) ... /* or !x or whatever */
> 
> In computing the value to be assigned to the variable `x', the compiler
> cannot short-circuit the expression.  This should be clear; in the
> simpler case
> 
>     x = (a | b | c);
> 
> if the variable `a' contains zero, the compiler must still OR the
> contents of `b' and `c' to determine the result.  These are bitwise
> logical operators.  Short-circuiting these makes no more sense than
> short-circuiting a sequence of multiplies as soon as one of the
> operands evaluates to `1'.
> 
> I of course agree that a potential problem exists with (1) above.  If
> the evaluation of `*cp++' is not atomic, and the increment could be
> delayed, the result may be equal to the first byte of the series.  In
> any case, `cp' must be incremented by `3' and both bitwise-OR operations
> must be performed.  Oh well, I guess an optimizing compiler that 
> delays the increments might realize this and skip the OR's...this
> is getting ridiculous.
> 
> --
> ****                                                         ****
> **** At Digital Lynx, we're almost in Garland, but not quite ****
> ****                                                         ****
> 
> Mike McNally                                    Digital Lynx Inc.
> Software (not hardware) Person                  Dallas  TX  75243
> uucp: {texsun,killer,infotel}!pollux!bobkat!m5  (214) 238-7474

This posting indicates a misunderstanding of how short-circuit evaluation
works. In the case of the '|' expression above, the decision to not evaluate
is would occur when a or b are all ones, NOT when a or b was zero. In the
case of the multiply, a or b == 0 would be used as the criterion. So the
question is not whether such expressions are capable of being short-circuited,
but whether they are. (Of course they are not.) Because logical boolean
operations are short-circuited, and binary boolean operations are not, an
expression like:

	if( cp[0] == '\0' && cp[1] == '\0' && cp[2] == '\0' ){
	    ...
	}

may be more efficient than:

	if( (*cp++ | *cp++ | *cp++) == 0){
	    ...
	}

and moreover, is guaranteed to work in any properly functioning C compiler
on any computer in the universe.


Stephen Watkins                    UUCP: ihnp4!datacube!stephen
Datacube Inc.; 4 Dearborn Rd.; Peabody, Ma. 01960; 617-535-6644

jtr485@umich.UUCP (Johnathan Tainter) (01/21/87)

In article <102600001@datacube>, stephen@datacube.UUCP writes:
>>     x = (a | b | c);
>> if the variable `a' contains zero, the compiler must still OR the
>> contents of `b' and `c' to determine the result.  These are bitwise
>> logical operators.  Short-circuiting these makes no more sense than
>> short-circuiting a sequence of multiplies as soon as one of the
>> operands evaluates to `1'.
>> Mike McNally                                    Digital Lynx Inc.
> This posting indicates a misunderstanding of how short-circuit evaluation
> works. In the case of the '|' expression above, the decision to not evaluate
> is would occur when a or b are all ones, NOT when a or b was zero.
You mean if the '|' had been a '||', of course.
And actually, when a or b is NONZERO not ALL ONES.
--j.a.tainter
> Stephen Watkins                    UUCP: ihnp4!datacube!stephen