Javascript Tilde & Two's complement

后端 未结 5 712
陌清茗
陌清茗 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:30

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

    Simply put, two's complement does not generate -(x + 1). One's complement does (i.e., ~ / bitwise NOT / flipping the bits).

    Two's compliment (flip the bits, add 1) is the (-0 averse) operation/encoding we use to express a negative number in pure bits (and derive it therefrom). Two's complement of x will generate -x.

    ~5 is nothing more than flipping the bits 0000 0101 to 1111 1010.

    To determine the value of 1111 1010, we flip back to 0000 0101 and add 1: 0000 0110 (-6).

提交回复
热议问题