Bitwise Unary NOT : Bitwise Operators in Java Programming

Bitwise Operators : Bitwise unary NOT

  • Bitwise Unary Not is also called as bitwise complement.
  • It is Unary Operator because it operates on single Operand.

Not Operator in Java Programming

  • Unary Not Operator is denoted by - ‘~‘.
  • The Unary NOT operator inverts all of the bits of its operand.
Number
42
Bit Pattern
00101010
After the NOT operator is applied
11010101
[468x60]

Live Example 1 : Bitwise unary NOT : Bitwise Operators

class BitwiseNOT{
  public static void main(String args[]){
  int ivar =42;
  System.out.println("~ ivar = " + ~ivar);
  }
}

Output :

~ ivar = -43
[468x60]

Live Example 2 : Unary Not Operator 0-9

class BitwiseNOT{
  public static void main(String args[]){
  System.out.println("~ 0 = " + ~0);
  System.out.println("~ 1 = " + ~1);
  System.out.println("~ 2 = " + ~2);
  System.out.println("~ 3 = " + ~3);
  System.out.println("~ 4 = " + ~4);
  System.out.println("~ 5 = " + ~5);
  System.out.println("~ 6 = " + ~6);
  System.out.println("~ 7 = " + ~7);
  System.out.println("~ 8 = " + ~8);
  System.out.println("~ 9 = " + ~9);
  }
}

Output :

~ 0 = -1
~ 1 = -2
~ 2 = -3
~ 3 = -4
~ 4 = -5
~ 5 = -6
~ 6 = -7
~ 7 = -8
~ 8 = -9
~ 9 = -10

Live Example : Bitwise Unary Not Applied to Boolean Variable

class Sample{
  public static void main(String args[]){
  boolean b1 = true;
  System.out.println("~ b1 = " + (~b1));
  }
}

Output :

Compile Error : operator ~ cannot be applied to boolean