what is this supposed to do?

Steve Langer langer at gibbs.uchicago.edu
Tue Apr 23 08:56:41 AEST 1991


Hi -- 
What is the expected output from the following program? Is it defined?

#include <stdio.h>

main() {
   printf("1 << 32 = %o\n", ((unsigned) 1) << 32);
}

Running on a Sun 4 gives 1.
Running on a Sun 3 gives 0.
Running on an Iris gives 1.

K&R (first edition) page 45 says that left shifts fill vacated bits by
0, which does not seem to be happening.

If the shift is done one step at a time the answer is 0 on the Sun 4 and
the Iris, as expected.

#include <stdio.h>

main() {
   int i;
   unsigned x = 1;
   for(i=0; i<32; i++) x <<= 1;
   printf("%o\n", x);
}

E-mail responses are welcome!
    Thanks in advance,
                   Steve
                   langer at control.uchicago.edu



More information about the Comp.lang.c mailing list