shift left arithmetic

Doug Gwyn gwyn at smoke.BRL.MIL
Tue Sep 11 02:08:58 AEST 1990


In article <2098 at charon.cwi.nl> aeb at cwi.nl (Andries Brouwer) writes:
>I stumbled over the following: 1<<32 produced 0, but
>int a; a=1; a<<32 produced 1. (Of course the word length
>of my machine is 32 bits.)
>Now I can understand how these values come about, and
>each is perfectly acceptable, but shouldnt static evaluation
>of expressions produce the same answers as dynamic evaluation?

NO.  When your exceed the bounds of what is guaranteed for the language,
well, there are simply no guarantees.  This example produces explicitly
undefined behavior, and the implementation is free to take whatever
apparently random actions it pleases.

Implementors note:  y=0;...;x<<y; is required to work by the C standard;
on some systems this will require a special-case test in the generated
code, since the hardware shift operation on those systems is incapable
of correctly shifting by 0 bit positions.  (I have seen such machines.)



More information about the Comp.std.c mailing list