Types of Bitwise Operators in Java
In this tutorial, we will learn about the various types or kinds of bitwise operators in java. Before proceeding to bitwise operators, let us know what is meant by the term operator. An operator is a symbol meant to perform a specific operation.
The following six types of bitwise operators are there in Java:
- Bitwise AND ( & )
- Bitwise exclusive OR (^)
- Bitwise inclusive OR (|)
- Bitwise Compliment ( ~ )
- Bitwise left Shift Operator ( << )
- Bitwise right Shift Operator ( >> )
Now, let us try to grasp the concept of each of the above bitwise operators.
Bitwise AND Operator
It is a binary operator denoted by the symbol &. If and only if both bits are 1, it returns 1otherwise it returns 0.
Let us now understand the working of bitwise AND operator through a java program.
Example 1:
public class BitwiseAndExample1
{
public static void main(String[] args)
{
int a = 6, b = 3;
System.out.println("a & b = " + (a & b));
}
}

Explanation: 6 = 0110 and 3 = 0011. Therefore, 6 & 3 = 0010 which is equal to 2
Example 2:
public class BitwiseAndExample2
{
public static void main(String[] args)
{
int a = 4, b = 2;
System.out.println("a & b = " + (a & b));
}
}
Output:

Explanation: 4 = 0100 and 2 = 0010. Therefore, 4 &2 = 0000 which is equal to 0
Bitwise Exclusive OR (^)
It is another binary operator which is denoted by the symbol ^ (caret). If both the bits are the same, it returns 0otherwise it returns 1.
Implementation:
Let us now understand the working of bitwise OR operator through a java program.
Example1:
public class BitwiseXorExample1
{
public static void main(String[] args)
{
int a = 6, b = 3;
System.out.println("a ^ b = " + (a ^ b));
}
}
Output:

Explanation: 6 = 0110 and 3 = 0011. Therefore, 6^3 = 0101 which is equal to 5
public class BitwiseXorExample1
{
public static void main(String[] args)
{
int a = 4, b = 2;
System.out.println("a ^ b = " + (a ^ b));
}
}
Output:

Explanation: 4 = 0100 and 2 = 0010. Therefore, 4 ^ 2 = 0110 which is equal to 6
Bitwise Inclusive OR (|)
It is another binary operator denoted by the symbol | (pipe). If either of the bit is 1, it returns the value 1otherwise it returns 0.
Implementation:
Let us see a java program to understand the working of the Bitwise inclusive OR operator.
Example 1:
public class BitwiseInclusiveOrExample1
{
public static void main(String[] args)
{
int a = 6, b = 3;
System.out.println("a | b = " + (a | b));
}
}
Output:

Explanation: 6 = 0110 and 3 = 0011. Therefore, 6 | 3 = 0111 which is equal to 7
Example2:
public class BitwiseInclusiveOrExample2
{
public static void main(String[] args)
{
int a = 4, b = 2;
System.out.println("a | b = " + (a | b));
}
}

Explanation: 4 = 0100 and 2 = 0010. Therefore, 4 | 2 = 0110 which is equal to 6
Bitwise Complement (~)
It is a unary operator which means it takes only 1 operand into account.It is denoted by the symbol ~ (tilde). The inverse or complement of the bit is returned by this operator. It thus changes every 0 to 1 and every 1 to 0.
Implementation:
Let us understand the working of a bitwise complement operator in a Java program.
Example 1:
public class BitwiseComplimentExample1
{
public static void main(String[] args)
{
int a = 6;
System.out.println("~a = " + (~a));
}
}

public class BitwiseComplimentExample1
{
public static void main(String[] args)
{
int a = 4;
System.out.println("~a = " + (~a));
}
}
Output:

Bitwise Shift Operators
The following three types of shift operators are offered in Java:
- Signed Left Shift Operator or Bitwise Left Shift Operator
- Signed Right Shift Operator or Bitwise Right Shift Operator
- Unsigned Right Shift Operator
Bitwise Left Shift Operators
The Left Shift operator is taken into account to shift the bits in the left direction as per the given no. of bits (n). It is denoted by a symbol <<. The shift operators can be used while dividing or multiplying any number by 2.It also conserves the leftmost bit (sign bit). It does not conserve the sign bit.
The following is the format or syntax to shift the bit:
variable << number of places to shift;
For example, if a=15
- a <<2; //shifts 2 bits
- a<< 3; //shifts 3 bits
- a << 5; //shifts 5 bits
Implementation:
Example 1:
Let us now look at some java programs to understand the concept of bitwise right shift operator.
public class SignedLeftShiftOperatorExample1
{
public static void main(String args[])
{
int a = 15;
System.out.println("a<<1 = " + (a << 1));
}
}
Output:

Example 2:
public class SignedLeftShiftOperatorExample2
{
public static void main(String args[])
{
int a = 20;
System.out.println("a<<1 = " + (a << 1));
}
}
Output:

Bitwise Right Shift Operators
The rightShift operator are taken into account to shift the bits in theright direction as per the stated number of bits (n). A symbol >> is used to denote it. The shift operators can be used while dividing or multiplying any number by 2. It also conserves the leftmost bit (sign bit). If 0 is existing at the leftmost bit, it can be inferred that the number is positive. If 1 is present at the leftmost bit, it denotes the number is negative.
The following is the format or syntax to shift the bit :
variable >>number of places to shift;
For example, if a=15
- a >>2; //shifts 2 bits
- a>> 3; //shifts 3 bits
- a >> 5; //shifts 5 bits
Implementation:
Example 1:
Let us now look at some java programs to understand the concept of bitwise right shift operator.
public class SignedLeftShiftOperatorExample1
{
public static void main(String args[])
{
int a = 15;
System.out.println("a<<1 = " + (a << 1));
}
}
Output:

Explanation: In the number 15, the 0 bits are shifted in the leftward direction as per the given no. of bits. This result in a doubling of the given number which in 30. The extra bits are wasted.
Example 2:
public class SignedLeftShiftOperatorExample2
{
public static void main(String args[])
{
int a = 20;
System.out.println("a<<1 = " + (a << 1));
}
}
Output:

Explanation: In the number 20, the 0 bits are shifted in the leftward direction as per the given no. of bits. This results ina doubling of the given number results in40. The extra bits are wasted.
Unsigned Right Shift Operator
It shifts a zero at the leftmost position and fills zeroes. The symbol >>>is used to denote it. It is to be noted that the leftmost position after >> depends on the sign bit. It does not conserve the sign bit.
Example 1:
If a=10000000 and b=1, find a>>>b?
a >>> b = 10000000 >>>1 = 01000000
Example2:
If a=11100000 and b= 3, find a>>>b?
a >>> b = 11100000 >>>3 = 00011100
Example3:
If a=11111100 and b=4, find a>>>b?
a >>> b = 11111100 >>>4 = 00001111
The left operand value is moved in the right direction as per the number of bits stated by the right operand and the shifted bits are occupied up with zeros. Extra bits shifted off in the right direction are rejected.
Implementation:
Example 1:
Let us see a Java program to understand the usage ofunsigned right-shift operators.
public class SignedRightShiftOperatorExample1
{
public static void main(String args[])
{
int a = 15;
System.out.println("a>>1 = " + (a >> 1));
}
}
Output:

Explanation: In the number 15, the 0 bits are shifted in the right direction as per the given no. of bits. This result in exactly half of the given number which is 7.
Example 2:
public class SignedRightShiftOperatorExample1
{
public static void main(String args[])
{
int a = 20;
System.out.println("a>>1 = " + (a >> 1));
}
}
Output:

Explanation: In the number 20, the 0 bits are shifted in the right direction as per the given no. of bits. This result in exactly half of the given number which is 10.
Summary
In this tutorial, we touched on various types of bitwise operators in Java language. We saw each type of bitwise operator through multiple examples and programs.