Java Tutorial Java Advanced Java Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

Java - Bitwise NOT operator



The Bitwise NOT operator (~) is an unary operator which takes a bit pattern and performs the logical NOT operation on each bit. It is used to invert all of the bits of the operand. It is interesting to note that for any integer x, ~x is the same as -(x + 1).

Bit~ Bit
01
10

The example below describes how bitwise NOT operator works:


  528 -> 00000000000000000000001000010000 (in binary)
        ----------------------------------
 -529 <- 11111111111111111111110111101111 (in binary) 


The code of using Bitwise NOT operator (~) is given below:

public class MyClass {
  public static void main(String[] args) {
    int x = 528;

    //Bitwise NOT operation
    int z = ~x;

    //Displaying the result
    System.out.println("x = "+ x);
    System.out.println("z = "+ z);
  }
}

The output of the above code will be:

x = 528
z = -529

❮ Java - Operators

5