What is the value of ~0 in C?

后端 未结 7 1390
暖寄归人
暖寄归人 2021-01-02 10:22

I want to get the values of INT_MIN and INT_MAX. I\'ve tried ~0 and ~0 >> 1 since the leftmost bit is a sign bit bu

7条回答
  •  走了就别回头了
    2021-01-02 10:52

    numbers are stored in 2's compliment so ~0 is 0XFFFFFFFF which is -1 .
    so FFFFFFF(1111) >>1 gives (1111)FFFFFFF = 0XFFFFFFFF = -1.

    When shifting an unsigned value, the >> operator in C is a logical shift. When shifting a signed value, the >> operator is an arithmetic shift.
    So , ~0U >> 1 gives (1111)FFFFFFF = 0XFFFFFFFF .

提交回复
热议问题