C code optimization for a = (b < c);

Lennart Augustsson augustss at cs.chalmers.se
Mon May 29 07:49:41 AEST 1989


In article <19787 at adm.BRL.MIL> WILCOX at nosc-tecr.arpa writes:
>
> ...
>     int a;
>     int b, c;
>     a = (b < c);
>
> [ should be replaced by ]
>
>a = (unsigned)((b - c) >> (WIDTH_OF_INT_IN_BITS - 1));
>
Good idea, but it doesn't work.  Assuming 16 bit integers, take
  b = -32768;	(0x8000)
  c = 32767;    (0x7fff)
now clearly b < c will evaluate to 1, but (unsigned)((b - c) >> 15 will
evaluate to 0, since b-c evaluates to 1 (0x0001).

	-- Lennart Augustsson


	Lennart Augustsson
Email:	augustss at cs.chalmers.se or augustss at chalmers.csnet



More information about the Comp.unix.wizards mailing list