question about shift operator

Gordon Cross crossgl at ingr.UUCP
Thu Nov 3 00:30:22 AEST 1988


In article <786 at gtx.com>, randy at gtx.com (Randy D. Miller) writes:
> I've seen conflicting information about the shift operator in cases
> where operand2 is exactly equal to the number of bits in the object.
> E.g., with a 32 bit int, what must the following do?
> 
>      int n;
>      n <<= 32;
> 
> K&R 1st ed. seems to indicate the operation is undefined, but does
> the proposed standard change this?  

K&R still applies.  The proposed ANSI standard states:

   "If the right operand is negative or is greater than or equal to the
    width in bits of the promoted left operand, the result is implementation
    defined."

Note the phrase "promoted left operand".  This means that (assuming 16-bit
shorts and 32-bit ints) the value of the expression "shortvar << 16" IS
defined and has the same value as "shortvar * 0x10000"!


Gordon Cross
Intergraph Corp.  Huntsville, AL



More information about the Comp.lang.c mailing list