13

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

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

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

Fine - lets go deeper.

Now lets talk about the Two's complement method.

5        = 0000 0101
Flip     = 1111 1010
add one  = 1111 1011

so 1111 1011 is -5.

how ?

again : flip :

0000 0100 

add one :

0000 0101

And so it was -5.

So how does this settle with ~5=-6 ?

where this -6 came from ?

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Royi Namir
  • 144,742
  • 138
  • 468
  • 792

6 Answers6

22

First of all, you need to realize that ~ is the bitwise flip operator, which is not the same as the negate operator -. ~ does only the bitwise flipping, but the negate operator - does bitwise flipping and add one (for integers).

As you've explained, if yo want to go from a postive number n to -n using the two complement method you bitwise flip/not n and add 1. ~n is just the bit-wise not meaning that ~n=-n-1.

For instance:

5               = 0000 0101
Flipped (~5)    = 1111 1010

So, which number does 1111 1010 represent? Since the first digit is a 1 we know it's a negative value. To find which value, do

-(flip(1111 1010) + 1) =
-(0000 0101 + 1)
-(0000 0110) =
-6
rofrol
  • 14,438
  • 7
  • 79
  • 77
Aleksander Blomskøld
  • 18,374
  • 9
  • 76
  • 82
10

~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?

Andreas Grapentin
  • 5,499
  • 4
  • 39
  • 57
3

~ is the Bitwise NOT operator (only inverts the bits of its operand).

For a positive number n,

~n + 1 = -n
xdazz
  • 158,678
  • 38
  • 247
  • 274
2

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).

Drazen Bjelovuk
  • 5,201
  • 5
  • 37
  • 64
  • 1
    this is the correct answer...I thought the same when I read all of the other wrong definitions of two complement – John Oct 17 '18 at 18:03
0

Tild(~) -

it is just flip(n). I.e. ~5 = flip(5). In java script numbers are always 64 bit signed. let us take Just 8 bit for reference,

 5==> 0000 0101 
~5 ==> filp(0000 0101)
~5 ==> 1111 1010 ==> -6 

2' complement -

It's filp(n) + 1.

5 ==> 0000 0101
2's complement of 5 ==> flip(0000 0101) + 0000 0001
2's complement of 5 ==> 1111 1010 + 000 0001
2's complement of 5 ==> 1111 1011
Naveenk
  • 78
  • 7
  • The [MDN doc](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_NOT) says that `~` converts the integer to 32-bit, discarding the more significant digits. The largest number I've found where `~x === -x -1` is `2**31 - 1`, which seems to bear that out. – BobRodes Mar 17 '21 at 23:27
0

5 = 0000 0101

Flip = 1111 1010

conversion break down 1111 1010

0 x 2^0 = 0
1 x 2^1 = 2
0 x 2^2 = 0
1 x 2^3 = 8
1 x 2^4 = 16
1 x 2^5 = 32
1 x 2^6 = 64
- 1 x 2^7 = -128

So -128+64+32+16+8+0+2+0 = -6