short circuit evaluation

chris at mimsy.UUCP chris at mimsy.UUCP
Sun Jan 25 03:45:20 AEST 1987


>>>    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.

Almost true.

>>>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.

Not so!

>In article <102600001 at datacube> stephen at datacube.UUCP writes:
>>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.

Correct.

In article <34 at umich.UUCP>jtr485 at umich.UUCP (Johnathan Tainter) writes:
>You mean if the '|' had been a '||', of course.

No, he means in the case of the `|' expression above.

>And actually, when a or b is NONZERO not ALL ONES.

Yes for `||', no for `|'.

C guarantees short-circuit left-to-right evaluation for `||' and
`&&'.  It makes no guarantees for `|' and `&'.  Here is a table
enumerating all possibilities.

    [For these, read `all zeroes bit pattern' for `OFF', and `all ones
     bit pattern' for `ON'.  This is a text compression device.]

      Given:	Possible methods of evalution:
      ------	------------------------------
	&&	Left side evaluated.  If zero, result is zero,
		and evaluation stops.  If nonzero, right side evaluated;
		result is 0 if zero, 1 if nonzer.

	||	Left side evaluated.  If nonzero, result is 1,
		and evaluation stops.  If zero, right side evaluated;
		result is 1 if nonzero, 0 if zero.

	&	1.  Left side evaluated.  If OFF, evaluation stops;
		    result is OFF.  If not, right side evaluated,
		    and both results ANDed.
		2.  Left side evaluated.  Right side evaluated.
		    Results ANDed.
		3.  Right side evaluated.  If OFF, evaluation stops;
		    result is OFF.  If not, left side evaluated, and
		    both results ANDed.

	|	1.  Left side evaluated.  If ON, evaluation stops;
		    result is ON.  If not, right side evaluated,
		    and both results ORed.
		2.  Left side evaluated.  Right side evaluated.
		    Results ORed.
		3.  Right side evaluated.  If ON, evaluation stops;
		    result is ON.  If not, left side evaluated, and
		    both results ORed.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
UUCP:	seismo!mimsy!chris	ARPA/CSNet:	chris at mimsy.umd.edu



More information about the Comp.lang.c mailing list