Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Operators in Java
3.
What are Bitwise Operators
4.
Bitwise OR
5.
Bitwise AND
6.
Bitwise XOR
7.
Bitwise Complement
7.1.
One’s Complement
8.
FAQs
9.
Key Takeaways
Last Updated: Mar 27, 2024

Bitwise Operator

Author APURV RATHORE
0 upvote

Introduction

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.

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

Operators in Java

Operators are special symbols in java that are used to perform operations on variables.

The operators in Java can be categorised as follows:

  • Arithmetic Operators
  • Shift Operators
  • Unary Operators
  • Bitwise Operators
  • Assignment Operator
  • Ternary Operator
  • Relational Operators
  • Logical Operators

In this article, we will discuss the bitwise operator in java. 

Also see, Swap Function in Java

Get the tech career you deserve, faster!
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);
    }
}

 

Output:

The result is 14

 

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

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

 

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

 

Output:

The result is -10

 

Practice it on online java compiler.

Check out this problem - XOR Queries On Tree

FAQs

1. What are operators in Java?

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. 

Read about Bitwise Operators in C here.

Happy Coding!

Previous article
Logical AND operator &&
Next article
Shift Operators in Java
Live masterclass