javascript bitwise operator question

前端 未结 4 574
渐次进展
渐次进展 2020-12-20 00:24

In Javascript when I do this

var num = 1;

~ num == -2

why does ~num not equal 0

in binary 1 is st

4条回答
  •  我在风中等你
    2020-12-20 01:02

    ~ toggles the bits of the operand so

    00000001
    

    becomes

    11111110
    

    which is -2

    Note: In javascript, the numbers are 32-bit, but I shortened it to illustrate the point.

提交回复
热议问题