Skip to main content

Bitwise Operators

  • These operators will be used to perform the operation on individual bits of the value.

Bitwise Operators Table

OperatorDescription
~Bitwise NOT
&Bitwise AND
|Bitwise OR
^Exclusive OR (XOR)
<<Left Shift
>>Right Shift
>>>Unsigned Right Shift

Operand Rules

  • Operands for the following Bitwise Operators can be of boolean type or integer type:
    • Bitwise AND (&)
    • Bitwise OR (|)
    • Exclusive OR (^)
  • Operands for the following Bitwise Operators will be of integer type only:
    • Bitwise NOT (~)
    • Left Shift (<<)
    • Right Shift (>>)
    • Unsigned Right Shift (>>>)

1's Complement

  • 1's complement of a binary number is formed by changing 1's to 0's and 0's to 1's.

Example:

Actual Bits : 00101101
1's Complement : 11010010

2's Complement

  • 2's complement of a binary number is formed by adding 1 to the least significant bit of 1's complement.

Example:

Actual Bits : 00101101
1's Complement : 11010010
Add 1 : 11010011
2's Complement : 11010011

Lab160.java

class Lab160 {

public static void main(String[] args) {
int a = 19;

System.out.println(a << 1);
System.out.println(a << 1);
System.out.println(a >> 1);
System.out.println(a >> 2);

int x = 15;
int y = 17;

System.out.println(x & y);
System.out.println(x | y);
System.out.println(x ^ y);
System.out.println(~x);
System.out.println(~y);

System.out.println(0101 << 1);
System.out.println(0101 >> 2);
}
}