Bitwise Operators in Java

The Bitwise operators in Java programming are used to perform bit operations. Instead of performing operations on numeric values, bitwise operators convert decimal values into binary values (a sequence of bits, including 0100, 1100, 1000, 1001, etc.).

The Java Bitwise Operators will work on these individual bits, such as comparing corresponding bits (bit by bit), shifting them left to right, or converting bit values from 0 to 1, etc. It is very helpful to perform low-level tasks directly on binary data. For instance, bit marking, manipulating bits, and so on.

List of Java Bitwise Operators

The following table shows the list of available (six) different bitwise operators with a simple example and meaning. For example, Consider x = 6 and y = 8, and their values in binary form are

x = 0110

y = 1000

Java Bitwise OperatorsMeaningExamples
&Bitwise ANDX & Y = 0000
|Bitwise ORX | Y = 1110
^Bitwise exclusive ORX ^ Y = 1110
~Bitwise complement~X = 00001001 (The Not operator will convert all 0 into 1). Remember, the Compliment of N will be -(N + 1)
<< Left Shift operatorX << 1 = 00001100 (Bits will move 1 step left. If we use 2 or 3, then they shift accordingly)
>> Right Shift operatorY >> 1 = 00000100
>>> Unsigned right shiftY >>> 1 = 00000100.

Java Bitwise Operators Truth Table

Let us see the Truth Table behind Bitwise Operators in this Programming Language.

xyx & yX | yx ^ y
00000
01011
10011
11110

Java Bitwise AND Operator (&)

The Bitwise AND operator, denoted by &, compares each bit of the given two numbers and returns 1 if both corresponding bits are 1. Otherwise (if any bit is 0 or both are 0s), it will return 0.

Syntax:

Res = a & b

Example

a = 3 = 0011

b = 2 = 0010

The Bitwise AND operator result of 3 and 2 is

0011 & 0010 = 0010 = 2 (Decimal)

As we know, if both bits are 1’s, then only the Bitwise AND will return True. In the above, only the bits in the second position from the right side are 1’s. So, it returns 1, and the remaining all are 0&1 and 1&0 = 0.

Bitwise AND Operator example

public class example {
public static void main(String[] args)
{
int a = 3;
int b = 2;
int c = a & b;
System.out.println("a & b = " + c );
}
}
a & b = 2

Java Bitwise OR Operator (|)

The Bitwise OR operator is denoted by |, which compares each bit of the two operands and returns 1 if either of the corresponding bits is 1.

Syntax

Res = a | b

Example

Let us perform the Bitwise OR operation on 5 and 2.

a = 5 = 0101

b = 2 = 0010

a | b = 0111 = 7

In the above binary values, starting from the left, the first bits are 0’s and 0|0 = 0. The next three corresponding bits are 1|0 and 0|1, and both of them return 1.

Bitwise OR Operator example

public class example {
public static void main(String[] args)
{
int a = 5;
int b = 2;

int c = a | b;
System.out.println("a | b = " + c );
}
}
a | b = 7

Java Bitwise XOR Operator (^)

The Bitwise XOR operator, denoted by ^, compares two binary numbers (bit by bit) and returns 1 only if the corresponding bits differ. For instance, 1 ^ 0 = 1.

Syntax

Res = a ^ b

Example

The bitwise XOR operation of 9 and 5 is

a = 9 = 1001

b = 5 = 0101

a ^ b = 1100 = 12 (Decimal)

In the above, the corresponding buts of the first two (left side) are 1^0 and 0^1. As they both differ, the result becomes 1. However, the other two (right side) bits are 0^0 and 1^ 1, as they are both the same, it returns 0.

Bitwise XOR Operator example

public class example {
public static void main(String[] args)
{
int a = 9;
int b = 5;

int c = a ^ b;
System.out.println("a ^ b = " + c );
}
}
a ^ b = 12

Bitwise Complement (~)

The Java Bitwise complement operator, denoted by ~, which will invert each bit of a given number (flipping 0 to 1 and 1 to 0).

Syntax

Res = ~a

Example

a = 4

As we all know, an int is a 32-bit signed integer type. So, the binary representation of 4 = 00000000 00000000 00000000 00000100

When we apply the Bitwise Complement (~0 operator, it converts those 0’s to 1 and t to 0.

~a = 11111111 11111111 11111111 11111011 = -5 (in decimal)

In short, we can say ~n = -(n + 1). So, ~4 = -(4 + 1) = -5.

Bitwise Complement Operator example

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

Example of all Bitwise operators in Java

This example helps to understand the Bitwise Operators practically. This program allows the user to enter two integer variables, a and b, and we use these two variables to show various operations.

import java.util.Scanner;

public class BitwiseOperators {
	private static Scanner sc;
	public static void main(String[] args) {
		int a, b;
		sc = new Scanner(System.in);
		System.out.println(" Please Enter two integer Value: ");
		a = sc.nextInt();
		b = sc.nextInt();
		
		System.out.format(" AND: %d & %d = %d \n", a, b, a & b);
		System.out.format(" OR : %d | %d = %d \n", a, b, a | b);
		System.out.format(" EXCLUSIVE OR: %d ^ %d = %d \n", a, b, a ^ b);
		System.out.format(" NOT : ~%d = %d \n", a, ~a);
		
		System.out.format(" LEFT SHIFT: %d << 1 = %d \n", a, a << 1);
		System.out.format(" RIGHT SHIFT: %d >> 1 = %d \n", b, b >> 1);
	}
}
Bitwise Operators in Java Example

The first three statements in this example will ask the user to enter integer values a and b. Next, we will assign the user input values to the variables.

The following System.out.format statements will perform all the table operations on a and b, then display the output.

In this Java Bitwise Operators example, we assigned the values as a = 12 and b = 25. The binary form of 12 = 00001100 and 25 = 00011001.

Let us see the Java calculations.

AND Operation = a & b
00001100 & 00011001 ==> 00001000 = 8

OR Operation = a || b
00001100 || 00011001 ==> 00011101 = 29

Next, the Exclusive OR Operation = a ^ b
00001100 || 00011001 ==> 00010101 = 21

Complement Operation = ~a
As we said before, the complement of N will be -(N + 1). It means – (12 + 1) = -13

Left Shift = a << 1
00001100 << 1 = 00011000 = 24

Right Shift = b >> 1
00011001 >> 1 = 00001100 = 12

Types of Bit Shift Operators in Java

The Bitwise shift operators are useful for shifting bits to the left or right by a given number of positions based on the specified number and the operator. There are three types of Java bitwise shift operators

  • Left shift operator.
  • Right Shift Operator.
  • Unsigned Right Shift Operator.

Left Shift Operator (<<)

The Bitwise left shift operator, denoted by <<, moves bits to the left side by a specified number of positions, and it fills the remaining positions with zero.

Syntax

Res = a << shift

Example

a = 6 and left shift by 1.

a = 6 = 0110. The left shift operator moves the bits to the left side by one position and fills the remaining gap with 0.

Result = 1100 = 12

Java Bitwise Left Shift Operator Example

public class example {
public static void main(String[] args)
{
int a = 6;
int b = a << 1;
System.out.println("a << 1 = " + b );
}
}
a << 1 = 12

Right Shift Operator (>>)

The Bitwise right shift operator, denoted by >>, moves bits to the right side by a specified number of positions, and it fills the remaining positions with zero. While moving bits to the right side, it preserves the sign bit for negative numbers.

Syntax

Res = a >> shift

Example

a = 6 and right shift by 1.

a = 6 = 0110. The right shift operator moves bits to the right side by one position and fills the remaining gap with 0.

Result =  0011 = 3 (in decimal)

Java Bitwise Right Shift Operator Example

public class example {
public static void main(String[] args)
{
int a = 6;
int b = a >> 1;
int c = -6;
int d = c >> 1;
System.out.println("a >> 1 = " + b );
System.out.println("c >> 1 = " + d );
}
}
a >> 1 = 3
c >> 1 = -3

Unsigned Right Shift (>>>)

The Java Bitwise unsigned right shift operator denoted by >>> does the same as the signed right shift operator. However, when moving the bits to the right side and filling the remaining values with 0s, it ignores the sign bit.

When it is a positive number, the result of the signed and unsigned right shift operators becomes the same. However, when it comes to a negative value, the result is different because the sign bit is ignored.

Syntax

Res = a >>> shift

Example

a = 4 = 0100 and shift value = 1.

As the value is a positive number, it simply shifts the bits to the right side by one position and fills the gap with 0.

Result = 0010 = 2 (in decimal).

Java Bitwise Unsigned Right Shift Operator Example

In the example below, we used the negative number with the unsigned right shift operator.

-4 >>> 1

The binary representation of -4 = 11111111 11111111 11111111 11111100 and right shift by 1.

Result = 01111111 11111111 11111111 11111110 = 2147483646 (In decimal).

public class example {
public static void main(String[] args)
{
int a = 4;
int b = a >>> 1;
int c = -4;
int d = c >>> 1;
System.out.println("a >>> 1 = " + b );
System.out.println("c >>> 1 = " + d );
}
}
a >>> 1 = 2
c >>> 1 = 2147483646

Common Bitwise Operations

The following are some real-time examples of how we can use Java bitwise operators to perform bitwise operations.

Use the Bitwise AND operator to check even or odd

Please refer to the Java program to Check If a number is even or odd.

public class example {
public static void main(String[] args)
{
int n = 10;
if ((n & 1) == 0) {
System.out.println(n + " is an even number");
}
else {
System.out.println(n + " is an odd number");
}
}
}
10 is an even number

Use Bitwise XOR to Swap two numbers

Please refer to the Java Program to Swap Two Numbers article.

public class example {
public static void main(String[] args)
{
int a = 10;
int b = 50;

a = a ^ b;
b = a ^ b;
a = a ^ b;

System.out.println("a = " + a + " and b = " + b);
}
}
a = 50 and b = 10