Java - Bitwise NOT Operator ~

What is Bitwise NOT Operator?

The bitwise NOT ~ operates on each bit of its operand. 1 is changed to 0 and 0 is changed to 1.

The bitwise NOT is also called a bitwise complement operator.

Consider the following code,

int i = ~13;

Java int uses 32 bits in 8-bit chunks in memory.

13 is 00000000 00000000 00000000 00001101

The value of ~13 can be computed as follows.

00000000 00000000 00000000 00001101
-----------------------------------
11111111 11111111 11111111 11110010

The result of ~13 is -14.

Demo

public class Main {
  public static void main(String[] args) {
    int i = ~13;/*from  w  w w. j  a  v a2  s. co  m*/
    System.out.println("i = " + i);
  }
}

Result