Is left and right shifting negative integers defined behavior?

后端 未结 3 2029
故里飘歌
故里飘歌 2020-11-27 18:24

I know, right shifting a negative signed type depends on the implementation, but what if I perform a left shift? For example:

int i = -1;
i << 1;
         


        
3条回答
  •  一个人的身影
    2020-11-27 19:20

    You're not reading that sentence correctly. The standard defines it if: the left operand has a signed type and a non-negative value and the result is representable (and previously in the same paragraph defines it for unsigned types). In all other cases (notice the use of the semicolon in that sentence), i.e, if any of these conditions isn't verified, the behaviour is undefined.

提交回复
热议问题