1. Introduction
Bit operators in Java are like secret agents working behind the scenes, manipulating individual bits of data to perform powerful operations. While they may seem mysterious at first, understanding how bit operators work is essential for unlocking the full potential of Java programming.
2. Integer BitWise Operators
Bitwise operators apply to each operand bit individually. Among these operators, only one is unary ~ (bitwise complement) and the others are binary: & (bitwise AND), | (bitwise inclusive OR), ^ (bitwise exclusive OR). You should note that the operators &, |, and ^ are overloaded. They can be applied to boolean operands to perform boolean logical operations.
The table below shows the semantics of these operators:
Operator | Notation | Meaning |
---|---|---|
Bitwise Complement | ~a | Invert the bit value. 1 becomes 0, 0 becomes 1 |
Bitwise AND | a & b | 1 if both a and b are 1, 0 otherwise |
Bitwise OR | a | b | 1 if either a or both a and b are 1, 0 otherwise |
Bitwise XOR | a ^ b | 1 if and only if one of the bits is 1, 0 otherwise |
Let’s consider two binary values a and b. The table below shows the result of applying each of these operators to the possible values of a and b.
a | b | ~a | a & b | a | b | a ^ b |
---|---|---|---|---|---|
1 | 1 | 0 | 1 | 1 | 0 |
1 | 0 | 0 | 0 | 1 | 1 |
0 | 1 | 1 | 0 | 1 | 1 |
0 | 0 | 1 | 0 | 0 | 0 |
2.1. Bitwise Operations Example
int a = 5;//0...101
int b = 4;//0...100
int complementA = ~a;//1...010 which is -6
int bitwiseAND = a & b;//0...100 which 4
int bitwiseOR = a | b;//0...101 which 5
int bitwiseXOR = a ^ b;//0...001 which is 1;
2.2. Bitwise Compound Assignment Operators: &=, |=, ^=
These operators are a shortcut to enable bitwise operation and assignment in one go. They can be defined as follows:
- a &= b is equivalent to a = a & b
- a |= b is equivalent to a = a | b
- a ^= b is equivalent to a = a ^ b
3. Shift Operators
These binary operators are used to perform bit shifts on the left operand. The right operand determines the number of bits to shift. Before proceeding with the shift, the bit distance is calculated by ANDing the right operand with the value 0x1f(31) if it is an integer operation. If the operation is carried out on long
type data, the AND mask is 0x3f(63). The consequence of all this is that the shift distance is always between 0 and 31 for int
types, and between 0 and 63 for long
types.
Operator | Notation | Meaning |
---|---|---|
Shift left | x << n | Shift all bits in x left n times, filling with 0 from the right |
Shift right with sign bit | x >> n | Shift all bits in x right n times, filling with the sign bit from the left |
Shift right with zero fill | x >>> n | Shift all bits in x right n times, filling with 0 from the left |
3.1. Shift Operations Example
int a = 4;//0...100
int b = 8;//0...1000
int c = -6; //1...010
int leftShift = a << 1;//0...1000 which is 8
int rightShiftWithSign = b >> 1;//0...100 which 4
int rightShiftWithZero = c >>> 1;//1...101 which 2147483645
Observations
- Each left shift of one bit corresponds to a multiplication by 2
- Each right shift of one bit with the sign bit corresponds to a division by 2
3.2. Shift Compound Assignment Operator: <<=, >>=, >>>=
Just like Bitwise operators, these operators are a shortcut to enable a bit shift operation and assignment in one go.
- a <<= n is equivalent to a = a << n
- a >>= n is equivalent to a = a >> n
- a >>>= n is equivalent to a = a >>> n
4. Operator Precedence and Associativity
Read about the Precedence and Associativity Rules for Bit Operators in this article.
5. Conclusion
Congratulations! You’ve now mastered the fundamentals of bit operators in Java. These tiny but mighty operators are the key to performing intricate bitwise operations and unleashing the full potential of your Java programs.
6. References
1) OCP Oracle Certified Professional Java SE 17 by Khalil A. Mughal and Vasily A. Strelnikov
2) Oracle Java Documentation
Pingback: Java Operators Decoded: Essential Tools for Crafting Robust Code