Unexpected output when executing left-shift by 32 bits

前端 未结 2 1116
情歌与酒
情歌与酒 2020-12-11 12:56

When I do a left shift of a hex I get -1 as output with the following code:

unsigned int i,j=0;
i= (0xffffffff  << (32-j));
printf(\"%d\",i);
         


        
2条回答
  •  时光说笑
    2020-12-11 13:42

    It's undefined behavior to left-shit 32 or greater on a 32-bit integer. That's what the error is about.

    C11 6.5.7 Bitwise shift operators

    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.

提交回复
热议问题