What is Bitwise Operator?
In Java, bitwise operators are used to perform operations at the bit level on several integer data types, like byte, short, int, long. They treat integers as sequences of bits and operate on each bit individually. This is a binary operator, this operator is symbolized by “ | ”. First, let’s see how this operator performs its work.
- The first step is to convert the operands to their binary representation.
- Then, the bitwise operator is applied to each binary number and calculates the result.
- At last, the result is again converted to its integer form.
Java provides several bitwise operators:
- Bitwise AND (&): It checks each bit of two numbers. If both numbers have a 1 in the same position, the result will have a 1 in that position. Otherwise, it will be 0.
- Bitwise OR (|): It also checks each bit of two numbers. If at least one of the numbers has a 1 in a particular position, the result will have a 1 in that position. Otherwise, it will be 0.
- Bitwise XOR (^): It checks each bit of two numbers. If the bits are different in a particular position, the result will have a 1 in that position. Otherwise, it will be 0.
- Bitwise NOT (~): It inverts all the bits of a single number. It turns 0 to 1 and 1 to 0.
- Left Shift (<<): It moves all the bits of a number to the left by a specified number of positions. It’s like multiplying the number by 2 raised to the power of the number of positions shifted.
- Right Shift (>>): It moves all the bits of a number to the right by a specified number of positions. It’s like dividing the number by 2 raised to the power of the number of positions shifted.
Example:
public class BitwiseOperatorExample { public static void main(String[] args) { int a = 5; // 0101 in binary int b = 3; // 0011 in binary // Bitwise AND int resultAND = a & b; // 0001 (1 in decimal) System.out.println("Bitwise AND: " + resultAND); // Bitwise OR int resultOR = a | b; // 0111 (7 in decimal) System.out.println("Bitwise OR: " + resultOR); // Bitwise XOR int resultXOR = a ^ b; // 0110 (6 in decimal) System.out.println("Bitwise XOR: " + resultXOR); // Bitwise NOT int resultNOTa = ~a; // 1010 (representing a two's complement negative value) System.out.println("Bitwise NOT of a: " + resultNOTa); // Left Shift int leftShiftResult = a << 2; // 010100 (20 in decimal) System.out.println("Left Shift: " + leftShiftResult); // Right Shift int rightShiftResult = a >> 1; // 0010 (2 in decimal) System.out.println("Right Shift: " + rightShiftResult); } }
Output:
Bitwise AND: 1/p>
Bitwise OR: 7
Bitwise XOR: 6
Bitwise NOT of a: -6
Left Shift: 20
Right Shift: 2
Explain:
Let’s explain the code so that you will easily understand the bitwise operator’s work.
- We have two integer variables `a` and `b`. `a` is set to 5, which is represented in binary as 0101, and `b` is set to 3, represented in binary as 0011.
- Bitwise AND (`&`): We use the `&` operator to perform a bitwise AND operation between `a` and `b`. It compares each bit of `a` with the corresponding bit of `b`. If both bits have a value of 1, the resulting value will also be 1 in that specific place; otherwise, it will be 0. The outcome of the bitwise AND operation between `a` and `b` is the binary representation 0001, which corresponds to the decimal value 1.
- Bitwise OR (`|`): We use the `|` operator to perform a bitwise OR operation between `a` and `b`. It compares each bit of `a` with the corresponding bit of `b`. If at least one bit is 1 in that position, the result will have a 1; otherwise, it will have a 0. The result of `a | b` is 0111 in binary, which is 7 in decimal.
- Bitwise XOR (`^`): We use the `^` operator to perform a bitwise exclusive OR (XOR) operation between `a` and `b`. It compares each bit of `a` with the corresponding bit of `b`. If the bits are different in that position, the result will have a 1; otherwise, it will have a 0. The result of `a ^ b` is 0110 in binary, which is 6 in decimal.
- Bitwise NOT (`~`): We use the `~` operator to perform a bitwise NOT operation on `a`. It flips all the bits of `a`. So, the result of `~a` is 1010 in binary, which represents a negative value in the two’s complement form.
- Left Shift (`<<`): We use the `<<` operator to perform a left shift on `a`. It moves all the bits of `a` to the left by 2 positions. This is equivalent to multiplying `a` by 2 raised to the power of 2. The result of `a << 2` is 010100 in binary, which is 20 in decimal.
- Right Shift (`>>`): We use the `>>` operator to perform a right shift on `a`. It moves all the bits of `a` to the right by 1 position. This is equivalent to dividing `a` by 2. The result of `a >> 1` is 0010 in binary, which is 2 in decimal.
Visited 1 times, 1 visit(s) today