Introduction:
Operators are the building blocks of programming languages, and Java offers a wide range of operators for performing various calculations and functions such as logical, arithmetic, relational, bitwise, etc. Bitwise operators are used to manipulate individual bits of a number and are used when performing update and query operations of Binary Indexed Trees. In this article, we will discuss Bitwise Operators in Java in detail, including their types and examples.
Types of Operators:
Before we dive into Bitwise Operators, it's essential to understand the different types of operators available in Java. Here are a few types:
- Arithmetic Operators
- Unary Operators
- Assignment Operator
- Relational Operators
- Logical Operators
- Ternary Operator
- Bitwise Operators
- Shift Operators
What are Bitwise Operators?
Bitwise operators are used to manipulate individual bits of a number. They can be used with any integral type (char, short, int, etc.). They perform bit-by-bit operations between two operands, and the result is stored in a new variable.
There are four main types of Bitwise Operators available in Java:
- Bitwise OR (|)
The bitwise OR operator is a binary operator, denoted by '|'. It returns bit-by-bit OR of input values, i.e., if either of the bits is 1, it returns 1, else it returns 0.
Example:
Let's say a=5 and b=7. In binary, a is 0101, and b is 0111.
The bitwise OR operation of a and b will be:
0101 | 0111
0111 = 7 (In decimal)
- Bitwise AND (&)
The bitwise AND operator is a binary operator, denoted by '&.' It returns bit-by-bit AND of input values, i.e., if both bits are 1, it returns 1, else it returns 0.
Example:
Let's say a=5 and b=7. In binary, a is 0101, and b is 0111.
The bitwise AND operation of a and b will be:
0101 & 0111
0101 = 5 (In decimal)
- Bitwise XOR (^)
The bitwise XOR operator is a binary operator, denoted by '^.' It returns bit-by-bit XOR of input values, i.e., if corresponding bits are different, it returns 1, else it returns 0.
Example:
Let's say a=5 and b=7. In binary, a is 0101, and b is 0111.
The bitwise XOR operation of a and b will be:
0101 ^ 0111
0010 = 2 (In decimal)
- Bitwise Complement (~)
The bitwise complement operator is a unary operator, denoted by '~.' It returns the one's complement representation of the input value, i.e., with all bits inverted. It makes every 0 to 1, and every 1 to 0.
Example:
Let's say a=5. In binary, a is 0101.
The bitwise complement operation of a will be:
~ 0101
1010 = 10 (In decimal)
Note: The compiler will give 2's complement of that number, i.e., 2's complement of 10 will be -6.
Examples:
Let's see some examples of how to use Bitwise Operators in Java.
Example 1:
// Java program to illustrate bitwise operators public class Operators { public static void main(String[] args) { int a = 5; int b = 7;
// Bitwise AND int c = a & b; System.out.println("a & b = " + c); // Output: a & b = 5
// Bitwise OR c = a | b; System.out.println("a | b = " + c); // Output: a | b = 7
// Bitwise XOR c = a ^ b; System.out.println("a ^ b = " + c); // Output: a ^ b = 2
// Bitwise complement c = ~a; System.out.println("~a = " + c); // Output: ~a = -6
// Left shift c = a << 2; System.out.println("a << 2 = " + c); // Output: a << 2 = 20
// Right shift c = a >> 2; System.out.println("a >> 2 = " + c); // Output: a >> 2 = 1
// Unsigned right shift c = a >>> 2; System.out.println("a >>> 2 = " + c); // Output: a >>> 2 = 1 } }
Output: a & b = 5 a | b = 7 a ^ b = 2 ~a = -6 a << 2 = 20 a >> 2 = 1 a >>> 2 = 1
In this program, we declared two integer variables "a" and "b" and assigned them the values 5 and 7, respectively. We then performed different bitwise operations on these variables.
The bitwise AND operator "&" compares the corresponding bits of "a" and "b" and sets the corresponding bit in the result to 1 only if both bits are 1. In this case, the result is 5.
The bitwise OR operator "|" compares the corresponding bits of "a" and "b" and sets the corresponding bit in the result to 1 if either bit is 1. In this case, the result is 7.
The bitwise XOR operator "^" compares the corresponding bits of "a" and "b" and sets the corresponding bit in the result to 1 only if one of the bits is 1 and the other is 0. In this case, the result is 2.
The bitwise complement operator "~" flips all the bits of "a" so that all 0's become 1's and all 1's become 0's. In this case, the result is -6 because the result is represented in 2's complement notation, which represents negative numbers using the leftmost bit.
The left shift operator "<<" shifts all the bits in "a" to the left by the specified number of bits (in this case, 2 bits). In this case, the result is 20 because the binary representation of 5 (which is "101") shifted left by 2 bits becomes "10100", which is equal to 20 in decimal notation.
The right shift operator ">>" shifts all the bits in "a" to the right by the specified number of bits (in this case, 2 bits). In this case, the result is 1 because the binary representation of 5 shifted right by 2 bits becomes "1", which is equal to 1 in decimal notation.
The unsigned right shift operator ">>>" is similar to the right shift operator ">>", except that it fills the leftmost bits of the result with 0's instead of copying the leftmost bit of "a". In this case, the result is also 1, because the binary representation of 5 shifted right by 2 bits with zero fill becomes "1", which is equal to 1