conditional expression evaluation question

Israel Pinkas pinkas at mipos3.UUCP
Wed Jan 14 03:22:06 AEST 1987


In article <207 at rebel.UUCP> george at rebel.UUCP (George M. Sipe) writes:
>
>I need to check a string, composed of byte triples, for a null area no
>less than MINSKIP triples in length.  A pointer, cp, is initialized to
>a triplet boundary.  After the test, it must remain on a triplet
>boundary.  Initially, I wrote the following:
>
>	while (cp < end && triples < MINSKIP)
>		if ((*cp++ | *cp++ | *cp++) == 0) ++triples;
>		else triples = 0;
> ...
>For now, I am using the following ugly code:
>
>	while (cp < end && triples < MINSKIP) {
>		if ((*cp | *(cp+1) | *(cp+2)) == 0) ++triples;
>		else triples = 0;
>		cp += 3;
>	}

Continue to use you ugly code.  C guarantees (at least K&R and H&S do) that
boolean expressions are evaluated only as much as possible, and left to
right.  As such, the first time *cp++ evaluates non-zero, the expression is
guaranteed to result in a non-zero value, and evaluation stops.  (You can
do wierd things like ((expr1 && expr2) || expr3); instead of
if (expr1) then expr2; else expr3; iff expr2 does not return a zero value.)
The first piece of code will actually find the first run of 3 * MINSKIP
consecutive null bytes, not guaranteed to fall on any boundary.  (This
assumes that your compiler is a "true C" compiler.  But that is another
matter.)

-Israel

-- 
----------------------------------------------------------------------
UUCP:	{amdcad,decwrl,hplabs,oliveb,pur-ee,qantel}!intelca!mipos3!pinkas
ARPA:	pinkas%mipos3.intel.com at relay.cs.net
CSNET:	pinkas%mipos3.intel.com



More information about the Comp.lang.c mailing list