Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!rutgers!clyde!ima!mirror!datacube!stephen From: stephen@datacube.UUCP Newsgroups: comp.lang.c Subject: Re: conditional expression evaluati Message-ID: <102600001@datacube> Date: Mon, 19-Jan-87 11:29:00 EST Article-I.D.: datacube.102600001 Posted: Mon Jan 19 11:29:00 1987 Date-Received: Tue, 20-Jan-87 23:44:20 EST References: <425@bobkat.UUCP> Lines: 60 Nf-ID: #R:bobkat.UUCP:-42500:datacube:102600001:000:2449 Nf-From: datacube.UUCP!stephen Jan 19 11:29:00 1987 > 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