49

Why bitwise operation (~0); prints -1 ? In binary , not 0 should be 1 . why ?

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
Sawyer
  • 15,581
  • 27
  • 88
  • 124

10 Answers10

79

You are actually quite close.

In binary , not 0 should be 1

Yes, this is absolutely correct when we're talking about one bit.

HOWEVER, an int whose value is 0 is actually 32 bits of all zeroes! ~ inverts all 32 zeroes to 32 ones.

System.out.println(Integer.toBinaryString(~0));
// prints "11111111111111111111111111111111"

This is the two's complement representation of -1.

Similarly:

System.out.println(Integer.toBinaryString(~1));
// prints "11111111111111111111111111111110"

That is, for a 32-bit unsigned int in two's complement representation, ~1 == -2.


Further reading:

polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
13

What you are actually saying is ~0x00000000 and that results in 0xFFFFFFFF. For a (signed) int in java, that means -1.

LaZe
  • 692
  • 4
  • 14
9

You could imagine the first bit in a signed number to be -(2x -1) where x is the number of bits.

So, given an 8-bit number, the value of each bit (in left to right order) is:

-128 64 32 16 8 4 2 1

Now, in binary, 0 is obviously all 0s:

    -128 64 32 16 8 4 2 1
0      0  0  0  0 0 0 0 0 = 0

And when you do the bitwise not ~ each of these 0s becomes a 1:

     -128 64 32 16 8 4 2 1
~0      1  1  1  1 1 1 1 1
 =   -128+64+32+16+8+4+2+1 == -1

This is also helpful in understanding overflow:

     -128 64 32 16 8 4 2 1
126     0  1  1  1 1 1 1 0  =  126
 +1     0  1  1  1 1 1 1 1  =  127
 +1     1  0  0  0 0 0 0 0  = -128  overflow!
nickf
  • 537,072
  • 198
  • 649
  • 721
8

~ is a bitwise operator.

~0 = 1 which is -1 in 2's complement form  

http://en.wikipedia.org/wiki/Two's_complement

Some numbers in two's complement form and their bit-wise not ~ (just below them):

0 1 1 1 1 1 1 1 = 127
1 0 0 0 0 0 0 0 = −128

0 1 1 1 1 1 1 0 = 126
1 0 0 0 0 0 0 1 = −127

1 1 1 1 1 1 1 1 = −1
0 0 0 0 0 0 0 0 = 0

1 1 1 1 1 1 1 0 = −2
0 0 0 0 0 0 0 1 = 1

1 0 0 0 0 0 0 1 = −127
0 1 1 1 1 1 1 0 = 126

1 0 0 0 0 0 0 0 = −128
0 1 1 1 1 1 1 1 = 127

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
N 1.1
  • 12,418
  • 6
  • 43
  • 61
  • +1 for the clear example. Programmers that like to puzzle can find out both how ~ works and how two's complement works just from carefully studying your example! – Stijn de Witt Sep 24 '13 at 19:58
5

Because ~ is not binary inversion, it’s bitwise inversion. Binary inversion would be ! and can (in Java) only be applied to boolean values.

Bombe
  • 81,643
  • 20
  • 123
  • 127
2

In standard binary encoding, 0 is all 0s, ~ is bitwise NOT. All 1s is (most often) -1 for signed integer types. So for a signed byte type:

0xFF = -1    // 1111 1111
0xFE = -2    // 1111 1110
...
0xF0 = -128  // 1000 0000
0x7F = 127   // 0111 1111
0x7E = 126   // 0111 1110
...
0x01 = 1     // 0000 0001
0x00 = 0     // 0000 0000
GManNickG
  • 494,350
  • 52
  • 494
  • 543
cletus
  • 616,129
  • 168
  • 910
  • 942
0

I think the real reason is that ~ is Two’s Complement.

Javascript designates the character tilde, ~, for the two’s complement, even though in most programming languages tilde represents a bit toggle for the one’s complement.

Nacho
  • 81
  • 3
0

It's binary inversion, and in second complement -1 is binary inversion of 0.

Daniel Fath
  • 16,453
  • 7
  • 47
  • 82
0

0 here is not a bit. It is a byte (at least; or more) - 00000000. Using bitwise or we will have 11111111. It is -1 as signed integer...

kpower
  • 3,871
  • 4
  • 42
  • 62
0

For 32 bit signed integer

~00000000000000000000000000000000=11111111111111111111111111111111 (which is -1)

YOU
  • 120,166
  • 34
  • 186
  • 219