What is the value of ~0 in C?

后端 未结 7 1518
暖寄归人
暖寄归人 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:46

    Based on the wikipedia article, C normally implements an arithmetic shift. That means that when you right-shift the quantity 0xffffffff, the left-most bit (sign bit) of 1 will be preserved, as you observe.

    However, Wikipedia also mentions the following, so you will get a logical shift (result of 0x7fffffff) if you use the unsigned type.

    The >> operator in C and C++ is not necessarily an arithmetic shift. Usually it is only an arithmetic shift if used with a signed integer type on its left-hand side. If it is used on an unsigned integer type instead, it will be a logical shift.

提交回复
热议问题