Is something wrong with the compiler ?

Ed Benyukhis benyukhi at motcid.UUCP
Fri Sep 28 03:09:31 AEST 1990


In article <143014 at sun.Eng.Sun.COM>, eager at ringworld.Eng.Sun.COM (Michael J. Eager) writes:
> In article <884 at gtenmc.UUCP> csp at gtenmc.UUCP (Charudutta S Palkar) writes:
> >      int a;
> >      printf(" Maxint : %d\na = %d\n", ( int )(( unsigned ) ~0 >> 1 ) , 
> >	      a = ( int )(( unsigned ) ( a = ~0 ) >> 1 ));
> > Output :
> >    Maxint : -1 
> >    a = 2147483647 
> >Can some complier writer tell me why this is happening ?
> 
> The right shift operator is not clearly defined when applied to 
> signed integers.  It looks like your compiler does a logical
> right shift, which inserts a high order zero bit.  




On the contrary, it looks like his compiler does an arithmetic shifts which
sign extends i.e. fills vacated bits with the original sign bit.  This problem
really concerns the "right shifts on signed operands".  The language permits
the shift to be implemented as an arithmetic shift operation or a logical
shift (fill vacated bits with 0's).  The choice is left to the compiler
writer.  "When the left operand of >> is an unsigned type, C mandates a
logical shift".  Thus declaring a variable to be an unsigned type yields
uniform behavior.  




Ed Benyukhis



More information about the Comp.lang.c mailing list