Java Bitwise Operators
Overview of Bitwise Operators
Java provides several bitwise operators that allow direct manipulation of individual bits within integer types. These operators work on the binary representation of numbers.
Types of Bitwise Operators
1. Bitwise AND (&)
Performs bit-by-bit AND operation between two numbers.
public class BitwiseAndExample {
public static void main(String[] args) {
int a = 0b1010; // 10 in decimal
int b = 0b1100; // 12 in decimal
int result = a & b;
System.out.println("a: " + Integer.toBinaryString(a));
System.out.println("b: " + Integer.toBinaryString(b));
System.out.println("a & b: " + Integer.toBinaryString(result));
}
}
2. Bitwise OR (|)
Performs bit-by-bit OR operation between two numbers.
public class BitwiseOrExample {
public static void main(String[] args) {
int a = 0b1010; // 10 in decimal
int b = 0b1100; // 12 in decimal
int result = a | b;
System.out.println("a: " + Integer.toBinaryString(a));
System.out.println("b: " + Integer.toBinaryString(b));
System.out.println("a | b: " + Integer.toBinaryString(result));
}
}
3. Bitwise XOR (^)
Performs bit-by-bit exclusive OR operation.
public class BitwiseXorExample {
public static void main(String[] args) {
int a = 0b1010; // 10 in decimal
int b = 0b1100; // 12 in decimal
int result = a ^ b;
System.out.println("a: " + Integer.toBinaryString(a));
System.out.println("b: " + Integer.toBinaryString(b));
System.out.println("a ^ b: " + Integer.toBinaryString(result));
}
}
4. Bitwise Complement (~)
Inverts all the bits of a number.
public class BitwiseComplementExample {
public static void main(String[] args) {
int a = 0b1010; // 10 in decimal
int result = ~a;
System.out.println("a: " + Integer.toBinaryString(a));
System.out.println("~a: " + Integer.toBinaryString(result));
}
}
Shift Operators
5. Left Shift (<<)
Shifts bits to the left, effectively multiplying by 2.
public class LeftShiftExample {
public static void main(String[] args) {
int a = 0b1010; // 10 in decimal
int result = a << 2;
System.out.println("a: " + Integer.toBinaryString(a));
System.out.println("a << 2: " + Integer.toBinaryString(result));
}
}
6. Right Shift (>>)
Shifts bits to the right, effectively dividing by 2.
public class RightShiftExample {
public static void main(String[] args) {
int a = 0b1010; // 10 in decimal
int result = a >> 2;
System.out.println("a: " + Integer.toBinaryString(a));
System.out.println("a >> 2: " + Integer.toBinaryString(result));
}
}
7. Unsigned Right Shift (>>>)
Shifts bits to the right, filling with zeros.
public class UnsignedRightShiftExample {
public static void main(String[] args) {
int a = -10;
int result = a >>> 2;
System.out.println("a: " + Integer.toBinaryString(a));
System.out.println("a >>> 2: " + Integer.toBinaryString(result));
}
}
Practical Bitwise Operator Applications
Bitwise Operator Comparison
Operator |
Symbol |
Description |
Example |
AND |
& |
Bit-by-bit AND |
1 & 0 = 0 |
OR |
| |
Bit-by-bit OR |
1 | 0 = 1 |
XOR |
^ |
Bit-by-bit XOR |
1 ^ 0 = 1 |
Complement |
~ |
Bit inversion |
~1 = 0 |
Left Shift |
<< |
Shift left |
1 << 1 = 2 |
Right Shift |
>> |
Shift right |
2 >> 1 = 1 |
Common Use Cases
graph TD
A[Bitwise Operators Use Cases]
A --> B[Flag Management]
A --> C[Optimization]
A --> D[Cryptography]
A --> E[Low-Level Programming]
LabEx recommends practicing these operators to gain a deeper understanding of bit-level manipulation in Java.