bitwise not operator

前端 未结 10 987
广开言路
广开言路 2021-01-30 16:24

Why bitwise operation (~0); prints -1 ? In binary , not 0 should be 1 . why ?

10条回答
  •  礼貌的吻别
    2021-01-30 17:11

    ~ is a bitwise operator.

    ~0 = 1 which is -1 in 2's complement form  
    

    http://en.wikipedia.org/wiki/Two's_complement

    Some numbers in two's complement form and their bit-wise not ~ (just below them):

    0 1 1 1 1 1 1 1 = 127
    1 0 0 0 0 0 0 0 = −128

    0 1 1 1 1 1 1 0 = 126
    1 0 0 0 0 0 0 1 = −127

    1 1 1 1 1 1 1 1 = −1
    0 0 0 0 0 0 0 0 = 0

    1 1 1 1 1 1 1 0 = −2
    0 0 0 0 0 0 0 1 = 1

    1 0 0 0 0 0 0 1 = −127
    0 1 1 1 1 1 1 0 = 126

    1 0 0 0 0 0 0 0 = −128
    0 1 1 1 1 1 1 1 = 127

提交回复
热议问题