Table of contents
1.
Introduction
2.
What is a Bitwise Operator in Java?
3.
Types of Bitwise Operators
3.1.
1. Bitwise OR (|)
3.2.
Java
3.3.
2. Bitwise AND (&)
3.4.
Java
3.5.
3. Bitwise XOR (^)
3.6.
Java
3.7.
4. Bitwise NOT
3.8.
Java
3.9.
5. Bitwise Complement (~)
3.9.1.
One’s Complement
3.10.
Java
3.11.
6. Bitshift Operators
3.11.1.
Bitwise Left Shift (<<)
3.12.
Java
3.12.1.
Bitwise Right Shift (>>)
3.13.
Java
3.13.1.
Unsigned Right Shift (>>>)
3.14.
Java
4.
Frequently Asked Questions 
4.1.
What is >> and >>> in Java?
4.2.
What is XOR equal to?
4.3.
How to declare a bit in Java?
4.4.
What is the difference between & (bitwise AND) and && (logical AND)?
4.5.
Can bitwise operators be used on non-integer types?
5.
Conclusion
Last Updated: Dec 6, 2024
Easy

Bitwise Operators in Java

Author APURV RATHORE
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Operators are one of the most fundamental ideas in Java, and knowing how to use them is essential. They enable you to perform operations on variables and values, such as arithmetic, logical comparisons, assignments, bitwise manipulations, and more, forming the backbone of programming logic.

In this tutorial, we'll learn more about Java's bitwise operators.

Bitwise Operators in Java

Recommended Topic-  Iteration Statements in Java, and Duck Number in Java.

What is a Bitwise Operator in Java?

Bitwise operators in Java perform operations on the binary representations of integers at the bit level. These operators manipulate individual bits within the integer values. Bitwise operators in Java perform operations directly on the binary representations of integers. They include:

  • AND (&): Sets each bit to 1 if both corresponding bits are 1.
  • OR (|): Sets each bit to 1 if at least one corresponding bit is 1.
  • XOR (^): Sets each bit to 1 if only one of the corresponding bits is 1.
  • Complement (~): Inverts all bits.
  • Left Shift (<<), Right Shift (>>), and Unsigned Right Shift (>>>) manipulate bit positions.

Types of Bitwise Operators

There are nine types of bitwise operator in Java:

  1. Bitwise OR
  2. Bitwise AND
  3. Bitwise XOR
  4. Bitwise NOT
  5. Bitwise Compliment
  6. Bitshift Operators
    1. Bitwise Left Shift
    2. Bitwise Right Shift
    3. Unsigned Right Shift Operator
NameSymbolSyntaxFunctionality
Bitwise OR|a | bPerforms bitwise OR: sets each bit to 1 if one of the bits is 1.
Bitwise AND&a & bPerforms bitwise AND: sets each bit to 1 if both corresponding bits are 1.
Bitwise XOR^a ^ bPerforms bitwise XOR: sets each bit to 1 if only one of the corresponding bits is 1.
Bitwise NOT~~aPerforms bitwise NOT: inverts all bits (changes 1s to 0s and 0s to 1s).
Bitwise Complement~~aThe bitwise complement operator (~) inverts all bits of a number. For example, ~a flips all the bits in the binary representation of a. It is equivalent to the bitwise NOT operation.
Bitshift Operators<<, >>, >>>a << n, a >> n, a >>> nShifts bits left or right by n positions. Left shift fills with zeros, right shift depends on the sign bit, and unsigned right shift fills with zeros.
Bitwise Left Shift<<a << nShifts bits of a to the left by n positions, filling with zeros.
Bitwise Right Shift>>a >> nShifts bits of a to the right by n positions, preserving the sign bit.
Unsigned Right Shift>>>a >>> nShifts bits of a to the right by n positions, filling with zeros, regardless of the sign bit.

1. Bitwise OR (|)

Bitwise OR operators, denoted by ‘|,’ return the bitwise OR of each bit two input numbers. If both bits are 0, it gives back 0; else, it gives back 1.

Example:

Let us calculate bitwise OR of 9 and 4.
bin(9) = 1001
bin(4) = 0100
_____________
         1101

 

  • Java

Java

class A {
   public static void main (String[] args) {
       int a = 9;
       int b = 4;
       int res = a|b;
       System.out.println("The result is "+ res);
   }
}
You can also try this code with Online Java Compiler
Run Code

Output:

The result is 13

2. Bitwise AND (&)

Bitwise AND operators, denoted by ‘&,’ return the bitwise AND of each bit two input numbers. If both bits are 1, it gives back 1; else, it gives back 0.

Example:

Let us calculate bitwise AND of 9 and 7.
bin(9) = 1001
bin(7) = 0111
_____________
         0001

 

  • Java

Java

class A {
   public static void main (String[] args) {
       int a = 9;
       int b = 7;
       int res = a&b;
       System.out.println("The result is "+ res);
   }
}
You can also try this code with Online Java Compiler
Run Code

Output:

The result is 1

3. Bitwise XOR (^)

Bitwise XOR operators, denoted by ‘^,’ return the bitwise XOR of each bit two input numbers. If both bits are different, it gives back 1; else, it gives back 0.

Example:

Let us calculate bitwise XOR of 9 and 7.
bin(9) = 1001
bin(7) = 0111
_____________
         1110

 

  • Java

Java

class A {
   public static void main (String[] args) {
       int a = 9;
       int b = 7;
       int res = a^b;
       System.out.println("The result is "+ res);
   }
}
You can also try this code with Online Java Compiler
Run Code

Output:

The result is 14

 

You can also read about the topic of Java Destructor and Hashcode Method in Java.

4. Bitwise NOT

The Bitwise NOT operator (~) in Java inverts the bits of its operand. It changes each bit to its opposite: 1s become 0s and 0s become 1s. This operator performs a bitwise complement operation.

Example
 

  • Java

Java

public class BitwiseNotExample {
public static void main(String[] args) {
int a = 5; // binary: 0000 0101
int result = ~a; // binary: 1111 1010

// In decimal, this result is -6 (two's complement representation)
System.out.println("Original value: " + a);
System.out.println("Bitwise NOT result: " + result);
}
}
You can also try this code with Online Java Compiler
Run Code

Output

Original value: 5 
Bitwise NOT result: -6

5. Bitwise Complement (~)

Before understanding the Bitwise Complement, let us first understand about one’s compliment. 

One’s Complement

The ones' complement of a binary number is the result of inverting all of the bits in the number's binary form (swapping 0s and 1s).

For example.

Let a = (10010). 
The one’s complement of a will be (01101).
a = 10010
——————————
b = 01101

 

Bitwise complement operators, denoted by ‘~’, return the one’s complement of the given input number. It traverses through each bit of the input number and makes it 1 if it is 0, else if it is 1 makes it 0.

Example:

Let us calculate complement of 9.
bin(9) = 1001
_____________
         0110

 

  • Java

Java

class A {
   public static void main (String[] args) {
       int a = 9;
       int res = ~a;
       // res will be 0110 in the binary representation, the compiler uses 2's
       // complement notation, hence it returns -10
       System.out.println("The result is "+ res);
   }
}
You can also try this code with Online Java Compiler
Run Code

Output:

The result is -10

6. Bitshift Operators

Bitshift operators in Java shift the bits of a number left or right by a specified number of positions. They are used to perform bitwise operations efficiently and manipulate the binary representation of integers.

Bitwise Left Shift (<<)

The Bitwise Left Shift operator (<<) shifts the bits of the number to the left by a specified number of positions. New bits on the right are filled with zeros. This operation effectively multiplies the number by 2n2^n2n, where nnn is the number of positions shifted.

Example:

  • Java

Java

public class BitwiseLeftShiftExample {
public static void main(String[] args) {
int a = 5; // binary: 0000 0101
int result = a << 2; // binary: 0001 0100 (20 in decimal)

System.out.println("Original value: " + a);
System.out.println("Bitwise Left Shift result: " + result);
}
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

Original value: 5
Bitwise Left Shift result: 20

Bitwise Right Shift (>>)

The Bitwise Right Shift operator (>>) shifts the bits of the number to the right by a specified number of positions. The sign bit (most significant bit) is preserved, which means the sign of the number (positive or negative) remains the same. This operation effectively divides the number by 2n2^n2n, where nnn is the number of positions shifted.

Example:

  • Java

Java

public class BitwiseRightShiftExample {
public static void main(String[] args) {
int a = 20; // binary: 0001 0100
int result = a >> 2; // binary: 0000 0101 (5 in decimal)

System.out.println("Original value: " + a);
System.out.println("Bitwise Right Shift result: " + result);
}
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

Original value: 20
Bitwise Right Shift result: 5

Unsigned Right Shift (>>>)

The Unsigned Right Shift operator (>>>) shifts the bits of the number to the right by a specified number of positions, filling the leftmost bits with zeros regardless of the sign bit. This operator is used for unsigned integers and treats the number as positive.

Example:

  • Java

Java

public class UnsignedRightShiftExample {
public static void main(String[] args) {
int a = -20; // binary (two's complement): 1110 1100
int result = a >>> 2; // binary: 0011 1110 (1073741820 in decimal)

System.out.println("Original value: " + a);
System.out.println("Unsigned Right Shift result: " + result);
}
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

Original value: -20
Unsigned Right Shift result: 1073741820

Practice it on online java compiler.

Check out this problem - XOR Queries On Tree

Frequently Asked Questions 

What is >> and >>> in Java?

>> is the signed right shift operator that preserves the sign bit, while >>> is the unsigned right shift operator that fills with zeros.

What is XOR equal to?

XOR, represented by ^, sets each bit to 1 if only one of the corresponding bits is 1, effectively performing an exclusive OR operation.

How to declare a bit in Java?

In Java, you can represent a bit using an integer, boolean, or bitwise operations. For example, int bit = 1; or boolean isBitSet = true;.

What is the difference between & (bitwise AND) and && (logical AND)?

& (bitwise AND) compares each bit of two numbers individually, performing a bitwise operation. && (logical AND) evaluates boolean expressions and short-circuits if the first condition is false, optimizing performance.

Can bitwise operators be used on non-integer types?

No, bitwise operators (&, |, ^, ~, <<, >>, >>>) can only be applied to integer types (byte, short, int, long) in Java. For other types, explicit conversion is required.

Conclusion

In this article, we have extensively discussed bitwise operators and their implementation in the Java programming language. We hope that this blog has helped you enhance your knowledge regarding bitwise operators and if you would like to learn more, check out our articles on Java

Recommended Articles:

Check out this problem - XOR Queries On Tree

Live masterclass