Javascript Tilde & Two's complement

后端 未结 5 720
陌清茗
陌清茗 2020-11-28 16:08

Two\'s complement method - generates -(x + 1).

for example when JavaScript encounters the Tilde he uses this method:

~5 = -(5+1) = -6         


        
5条回答
  •  难免孤独
    2020-11-28 16:39

    ~5 = -(5 + 1) = -6

    so far so good. However, ~ is not the two's complement, it's the binary inversion operator.

    5     = 0000 0101
    flip  : 1111 1010
    

    which is -6

    does that make it clear?

提交回复
热议问题