C history question

Barry Margolin barmar at think.COM
Sat Sep 16 05:32:12 AEST 1989


Regarding the lack of the ability to abbreviate

	allok = allok && (a[i] > b[i])
with
	allok &&= a[i] > b[i]

In article <10390 at phoenix.Princeton.EDU> tbrakitz at phoenix.Princeton.EDU (Byron Rakitzis) writes:
>Why not
>	       allok &= (a[i] > b[i]);
>In this case, the expression on the right will evaluate to either 0
>or 1, and you can AND this with the previous value of allok.

That's not a good substitute, for two reasons.

First, because && and !! treat all non-zero values as boolean truth,
but & does bit-wise AND.  If allok contained 2 before the above
expression it would end up with 0 (whether or not the comparison is
true or false!).  I admit that such code is not a good idea, and your
version may actually work in the poster's case.

Second, and more importantly, is that your version doesn't retain the
short-circuit operation.  If the expression were something like

	allok = allok && a[i++] > b[j++]

then the side effects on i and j would be different from

	allok &= a[i++] > b[j++]

The first must NOT increment i and j when allok is true, while the
second MUST increment them in any case.


Barry Margolin
Thinking Machines Corp.

barmar at think.com
{uunet,harvard}!think!barmar



More information about the Comp.lang.c mailing list