How does the bitwise complement operator (~ tilde) work?

后端 未结 15 1583
无人共我
无人共我 2020-11-22 07:46

Why is it that ~2 is equal to -3? How does ~ operator work?

15条回答
  •  没有蜡笔的小新
    2020-11-22 08:17

    This operation is a complement, not a negation.

    Consider that ~0 = -1, and work from there.

    The algorithm for negation is, "complement, increment".

    Did you know? There is also "one's complement" where the inverse numbers are symmetrical, and it has both a 0 and a -0.

提交回复
热议问题