Do you think IIT Guwahati certified course can help you in your career?
No
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 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:
Bitwise OR
Bitwise AND
Bitwise XOR
Bitwise NOT
Bitwise Compliment
Bitshift Operators
Bitwise Left Shift
Bitwise Right Shift
Unsigned Right Shift Operator
Name
Symbol
Syntax
Functionality
Bitwise OR
|
a | b
Performs bitwise OR: sets each bit to 1 if one of the bits is 1.
Bitwise AND
&
a & b
Performs bitwise AND: sets each bit to 1 if both corresponding bits are 1.
Bitwise XOR
^
a ^ b
Performs bitwise XOR: sets each bit to 1 if only one of the corresponding bits is 1.
Bitwise NOT
~
~a
Performs bitwise NOT: inverts all bits (changes 1s to 0s and 0s to 1s).
Bitwise Complement
~
~a
The 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 >>> n
Shifts 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 << n
Shifts bits of a to the left by n positions, filling with zeros.
Bitwise Right Shift
>>
a >> n
Shifts bits of a to the right by n positions, preserving the sign bit.
Unsigned Right Shift
>>>
a >>> n
Shifts 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
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
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
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
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
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)
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)
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)
>> 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.