Bitwise operators in Java let you change numbers at the bit level. They are commonly used in system-level programming, performance optimization, encryption algorithms, and graphics processing. Java provides bitwise operators like AND, OR, XOR, NOT, left shift, and right shift for bit-level operations.
In this blog, we will learn how bitwise operators work and how Java represents positive and negative numbers in binary can help you write faster and more efficient code. However, bitwise logic can be tricky, so always test your operations carefully.
Table of Contents:
What Are Bitwise Operators in Java?
The bitwise operators in Java are the operators that are used to perform the operation at the bit level. When the operation using the bitwise operator is performed, every bit is considered as an individual bit.
These Java operators are mainly used in low-level programming, encryption, and performance optimization, where direct manipulation of bits is required.
The bitwise operators are mainly divided into two categories, these are,
- Unary Operators: These operators work with only one operand. The bitwise NOT (~) is a unary bitwise operator.
- Binary Operators: These operators work with two operands. They include operators such as AND, OR, XOR, Left Shift, Right Shift, and Unsigned Right Shift.
Note: When using bitwise operators, always be aware of the data type size to prevent overflow issues.
Master Java Today - Accelerate Your Future
Enroll Now and Transform Your Future
Types of Bitwise Operators in Java
There are many types of Bitwise operators in Java. Some of them are as follows.
Operator Type |
Symbol |
Syntax |
Description |
Bitwise AND |
& |
a & b |
Returns 1 only if both bits are 1 |
Bitwise OR |
| |
a | b |
Returns 1 if either bit is 1 |
Bitwise XOR |
^ |
a ^ b |
Returns 1 if bits are different |
Bitwise NOT |
~ |
~a |
Inverts all bits (1’s complement) |
Left Shift |
<< |
a << n |
Shifts bits left, fills with 0 |
Signed Right Shift |
>> |
a >> n |
Shifts right preserving sign bit |
Unsigned Right Shift |
>>> |
a >>> n |
Shifts right filling with 0 |
Now, let us discuss all of the above in detail.
1. Bitwise AND
First, let us discuss the truth table of the AND operator, which will help us to understand the Bitwise AND operators easily.
Bit A |
Bit B |
A & B |
0 |
0 |
0 |
0 |
1 |
0 |
1 |
0 |
0 |
1 |
1 |
1 |
The Bitwise AND operator gives 1 only if both bits are 1, otherwise, it gives 0.
Syntax:
result= A & B ;
Example:
Output:
In the above Java program, the variables a and b have been assigned the values 5 and 3. These values are first converted to binary values, and after which the bitwise AND operation is performed at each bit.
2. Bitwise OR
First, let us discuss the truth table of the OR operator, which will help us to understand the Bitwise OR operators easily.
Bit A |
Bit B |
A | B |
0 |
0 |
0 |
0 |
1 |
1 |
1 |
0 |
1 |
1 |
1 |
1 |
The Bitwise OR operator gives 1 if at least one of the bits is 1; otherwise, it gives 0.
Syntax:
result= A | B ;
Example:
Output:
In the above Java program, the variables a and b have been assigned the values 5 and 3. These values are first converted to binary values, and after which the bitwise OR operation is performed at each bit.
3. Bitwise XOR
First, let us discuss the truth table of the XOR operator, which will help us to understand the Bitwise XOR operator easily.
Bit A |
Bit B |
A ^ B |
0 |
0 |
0 |
0 |
1 |
1 |
1 |
0 |
1 |
1 |
1 |
0 |
The Bitwise XOR operator gives 1 if the bits are different, otherwise, it gives 0.
Syntax:
result = A ^ B;
Example:
Output:
In the above Java program, the variables a and b have been assigned the values 5 and 3. These values are first converted to binary values, and after which the bitwise XOR operation is performed at each bit.
4. Bitwise Complement
First, let us discuss the truth table of the Complement operator, which will help us to understand the Bitwise Complement operator easily.
The Bitwise Complement operator flips each bit: 1 becomes 0, and 0 becomes 1. It does not require two operands, it operates on a single value.
Note: In Java, integers are stored in two’s complement form. So, ~a is equal to -(a + 1).
Syntax:
result = ~A;
Example:
Output:
In the above Java program, the variable a has been assigned the value 5. This value is first converted to binary. After this, the complement operator is used at the binary level.
Unlock Your Future in Java
Start Your Java Journey for Free Today
Bitwise Shift Operators
In Java, bitwise shift operators allow you to shift the bits of a number to the left or right.
These operators work on the binary format of the numbers, like how computers internally store their data. There are three primary shift operators in Java. These are:
1. Left Shift Operator
The Left Shift operator shifts all the bits to the left and then places 0 at the right.
When the number is shifted by one position, it is like multiplying the number by 2, i.e, every time you shift to the left, the value of the number gets doubled. It can be used to increase the value of a number.
Syntax:
result = A << n;
Where n is the number of positions to be shifted
Example:
Output:
In the above Java program, the value of a is converted to binary format. Then the Left Shift operator is used, which shifts the bits of a one position to the left, and a zero is added at the rightmost bit.
2. Right Shift Operator
The Right Shift operator moves the bits to the right and keeps the sign bit for the negative numbers.
Syntax:
result = A >> n;
Where n is the number of positions to be shifted
For Positive Numbers:
When you apply the Right Shift (>>) operator to a positive number, the bits are shifted to the right, and the leftmost bits are filled with 0 (zero). This is because the sign bit (the leftmost bit) doesn’t need to be kept for the positive numbers.
Example:
Output:
For Negative Numbers:
When you apply the Right Shift (>>) operator to a negative number, the leftmost bits are filled with the sign bit (1 for negative numbers). This keeps the sign of the number, keeping it negative after the shift.
Example:
Output:
In the above Java program, the value of a is converted to binary format. Then the Right Shift operator is used, which shifts the bits of a one position to the right, and a zero is added at the leftmost bit.
3. Unsigned Right Shift or Zero-fill Right Shift
The Unsigned Right Shift operator shifts the bits to the right and left and fills the leftmost bits with 0 and ignores the sign.
Syntax:
result = A >>> n;
Where n is the number of positions to be shifted
Example:
Output:
In the above Java program, the value of a is converted to binary format. Then the Unsigned Shift operator is used, which shifts the bits and fills the leftmost bit with 0, except the sign bit.
Note: Java doesn’t support the Unsigned Left Shift Operator
4. Signed Right Shift
The Signed Right Shift operator shifts the bits to the right, but it preserves the sign bit (i.e., it fills the leftmost bits with the sign bit, 1 for negative numbers and 0 for positive numbers.
Syntax:
result = A >> n;
Where n is the number of positions to be shifted
Example:
Output:
In the above Java program, the value of a is converted to binary format. Then the signed Right Shift operator is used, which shifts the bits of the positive number a one position to the right.
Understanding the 2’s Complement Twist in the Bitwise Complement Operator in Java
The twist in the Bitwise complement operator is how it behaves differently with the positive and negative binary values. When you apply the complement to the number, it reverses all its bits, also it changes the value of a number. Here comes the concept of two’s complement to represent negative numbers.
- For Positive Numbers, when you use the complement on a positive number, the result becomes negative.
- For Negative Numbers, when you use the complement on a negative number, the result becomes positive.
It happens because Java uses two’s complement to represent the negative numbers. So, when you reverse the bits of a positive or negative number, you also reverse its sign and change its value in such a way that it follows the two’s complement system.
Formula:
- Bitwise complement of N = ~N, which is represented in two’s complement form.
- Two’s complement of ~N = -(~(~N) + 1), which simplifies to -(N + 1).
Where N is a number.
Example:
Output:
In the above Java program, the values of a and b are converted to binary. Then, the Bitwise Complement operator is applied to each value. The operator reverses each bit of the number.
- When the Bitwise Complement operator is applied to 5, it flips each bit, which represents -6 in two’s complement form.
- When the Bitwise Complement operator is applied to -5, it flips each bit, which represents 4 in decimal.
Advantages And Disadvantages Of Using Bitwise Operators In Java
Advantages of Bitwise Operators in Java
- They are fast: Bitwise operations deal at the binary level, so they run super quickly, and are faster than regular math operations.
- They help save memory: You can combine many true or false values, like flags, into a single number. It’s a neat trick when you want to keep things clear.
- You get fine control: Bitwise operators work with a single bit, hence, they let you change individual bits on or off, which will be easy to handle when you need low-level control.
- Great for performance tuning
In areas like encryption, graphics, or compression, bitwise logic can help achieve better performance.
- Useful for working with flags: Instead of using many variables, you can use one number to see multiple options and check them with the simple logic.
Disadvantages of Bitwise Operators in Java
- They’re not easy to read: Code with bitwise logic can look complex, especially if you are not familiar with binary or bit manipulation.
- Easy to mess up: One wrong shift or mask can change things, and it might not be the right way where the problem is.
- No guardrails: The Bitwise code works with binary numbers, so the compiler will not catch any type-related mistakes, so you have to be careful.
- It can be hard to maintain: The code with the bitwise operator is very difficult to understand and update. One error of 1 and 0 can be very challenging
Real-World Use Cases of Logical Operators in Java
1. Low-Level Programming
Bitwise operators are often used in low-level programming techniques, when working with hardware, doing memory-level work, or with binary data, like in embedded systems.
2. Speed and Performance
Bitwise operations are fast because they work at the bit level. Hence, they are useful in increasing the performance of applications, like games.
3. Masking Values
You can use the AND (&) operator to keep only the bits you want. This is called masking,it is used to get the specific bits from a number.
4. Toggling Bits
The XOR operator can flip bits from 0 to 1 or 1 to 0. It provides an easy way to toggle specific bits.
5. Cryptography
Bitwise XOR is used in encryption and decryption practices. It hides the data by combining it with the keys in a simple way.
Get 100% Hike!
Master Most in Demand Skills Now!
Conclusion
Bitwise operators in Java work with the individual bits. They are powerful tools and are used in areas like system programming, performance, encryption, and graphics. If you are performing bit-level calculations with AND, OR, XOR, or shifting bits left or right, these operators can improve the efficiency of the code.
If you want to learn more about Java, you can refer to our Java course.
Bitwise Operators in Java – FAQs
Q1. What are the bitwise operators in Java?
Bitwise operators are the operators in Java that provide a way to manipulate data at the bit level.
Q2. What are SAS operators?
A SAS operator is a symbol that is used to perform a comparison, arithmetic calculation, or logical operation.
Q3. What is bitwise not in Java?
The bitwise NOT operator returns a number or BigInt whose binary representation has a 1 in each bit position for which the corresponding bit of the operand is 0, and a 0 otherwise.
Q4. What is a special operator?
Special operators are symbols that perform tasks beyond standard arithmetic and logical operations. They are used for specific purposes like comparing object identity, managing memory, working with pointers, or accessing structure members.
Q5. What is an LSB?
The LSB refers to the rightmost bit in a binary number representation. It holds the lowest value in the binary place value system.