why shift int a=1 to left 31 bits then to right 31 bits, it becomes -1

前端 未结 4 1157
别那么骄傲
别那么骄傲 2021-01-29 07:19

given

int a = 1; (00000000000000000000000000000001),

what I did is just

a=(a<<31)>>31;

4条回答
  •  抹茶落季
    2021-01-29 07:33

    " however it turns out to be -1"

    You use an unsigned int to do so for seeing only 32 bit values that are greater than 0:

     unsigned int a = 1; 
     a=(a<<31)>>31;  
    

提交回复
热议问题