On 4/13/07, Marcus Meissner <[EMAIL PROTECTED]> wrote:
On Fri, Apr 13, 2007 at 05:38:03AM -0400, Ken Takusagawa wrote:
> What's going on here?  I'm expecting the answer 0, but get 2.
>
> #include <stdio.h>
> int main(){
>  unsigned x=1;
>  printf("%u\n",(x<<33));
>  /* outputs "2" on gcc 4.1.2 on x86_32 */
>
>  /*
>    [#4] The result of E1  <<  E2  is  E1  left-shifted  E2  bit
>    positions; vacated bits are filled with zeros.  If E1 has an
>    unsigned type, the value of the result  is E1*2^E2,  reduced
>    modulo  one more than the maximum value representable in the
>    result type.
>
>    http://std.dkuug.dk/JTC1/SC22/WG14/www/docs/n843.htm
>  */

Read from just 1 paragraph above the last sentence:

[#3] The integer promotions are performed  on  each  of  the
     operands.   The  type  of the result is that of the promoted
     left operand.  If the value of the right operand is negative |
     or  is  greater  than  or equal to the width of the promoted
     left operand, the behavior is undefined.

Ciao, Marcus


I see, so in this case, the undefined behavior happens to return "2".

Reply via email to