-2

int x=10;

System.out.println(~x);

//this will print -11

//how to do the calculation manually using complement arithmetic

2 Answers2

1

This is a Negation operator it will consider ~x= -(10+1), so you will get -11 as the output. Refer some C books you can get more explanation on this

sidhartha pani
  • 623
  • 2
  • 12
  • 23
0

Perhaps this helps you: You can print out the bits of the integer as following. There you can see that a int is represented as a 32-bit value. Explanations about bitwise not operator can be found on the web i guess;

    int x = 10;
    System.out.println(Integer.toBinaryString(x));  //00000000000000000000000000001010
    System.out.println(Integer.toBinaryString(~x)); //11111111111111111111111111110101
    System.out.println(~x); //-11
Markus
  • 1,141
  • 1
  • 9
  • 25