Two\'s complement method - generates -(x + 1)
.
for example when JavaScript encounters the Tilde he uses this method:
~5 = -(5+1) = -6
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)
.