bitwise not operator

53,950

Solution 1

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:

Solution 2

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

Solution 3

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!

Solution 4

~ 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

Solution 5

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

Share:
53,950

Related videos on Youtube

Sawyer
Author by

Sawyer

Updated on February 10, 2020

Comments

  • Sawyer
    Sawyer over 4 years

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

    • kennytm
      kennytm over 14 years
      If you want to flip a single bit, use x ^ 1.
    • user207421
      user207421 over 14 years
      It's not a 'not' operator. It is a 'complement' operator.
    • kennytm
      kennytm over 14 years
      @EJP: A one's complement operator.
    • user207421
      user207421 over 14 years
      No it isn't. The language specification #4.2.2 defines "~" as 'the bitwise complement operator'. There is no such thing in Java as a 'bit operator for NOT'.
    • kennytm
      kennytm over 14 years
      @lh3: No. It's a one's complement operator in both C and C++.
  • Stijn de Witt
    Stijn de Witt almost 11 years
    +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!