Java is one of the most widely used programming languages worldwide. Learning Java enhances critical computer science principles and opens the door to a wide range of professional options.

Operators are one of the most fundamental ideas in Java, and knowing how to use them is essential.

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

Connect with our expert counsellors to understand how to hack your way to success

User rating 4.7/5

1:1 doubt support

95% placement record

Akash Pal

Senior Software Engineer

326% Hike After Job Bootcamp

Himanshu Gusain

Programmer Analyst

32 LPA After Job Bootcamp

After Job Bootcamp

What are Bitwise Operators

A bitwise operator is an operator that manipulates individual bits in bit patterns or binary numbers to execute bitwise operations. Bitwise operators are used to manipulate the individual bits of a number. They're compatible with any integral type (short, int, etc.). They are also useful in a number of other data structures, such as binary indexed trees.

Now we are going to discuss the types of bitwise operators.

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

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);
}
}

Output:

The result is 13

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

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);
}
}

Output:

The result is 1

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

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);
}
}

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

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);
}
}

Ans: Operators are special symbols that are used to perform operations on two or more variables and then return the results.

2. What are bitwise operators in Java?

Ans: A bitwise operator is an operator that manipulates individual bits in bit patterns or binary numbers to execute bitwise operations. Bitwise operators are used to manipulate the individual bits of a number. 3. What are the types of bitwise operators?

Ans: Bitwise operators are of four types: Bitwise AND, Bitwise OR, Bitwise XOR and Bitwise NOT.

Key Takeaways

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. Do upvote our blog to help other ninjas grow.