Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!rutgers!lll-lcc!seismo!mimsy!chris From: chris@mimsy.UUCP Newsgroups: comp.lang.c Subject: Re: short circuit evaluation Message-ID: <5178@mimsy.UUCP> Date: Sat, 24-Jan-87 12:45:20 EST Article-I.D.: mimsy.5178 Posted: Sat Jan 24 12:45:20 1987 Date-Received: Sun, 25-Jan-87 05:44:12 EST References: <425@bobkat.UUCP> <102600001@datacube> <34@umich.UUCP> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 66 >>> 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@datacube> stephen@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@umich.UUCP>jtr485@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@mimsy.umd.edu