Java bitwise operators allow you to manipulate individual bits of integer types (such as int, short, byte, and long).
These operators are less commonly used than arithmetic and relational operators, but they are crucial in low-level programming, such as system programming, cryptography, network programming, and bit manipulation tasks.
In this tutorial, we will walk through Java's bitwise operators, provide code examples, and explain how these operators work with binary values.
Table of Contents:
1. Introduction to Bitwise Operators
Bitwise operators perform operations on binary representations of integers. These operators treat operands as sequences of bits (binary digits) rather than as decimal, hexadecimal, or octal numbers. Bitwise operations are often used when you need to manipulate specific bits within a number or when working with binary protocols.
2. List of Java Bitwise Operators
Java provides several bitwise operators:
Operator | Name | Description |
---|---|---|
& |
Bitwise AND | Performs AND operation on each bit |
` | ` | Bitwise OR |
^ |
Bitwise XOR | Performs XOR operation on each bit |
~ |
Bitwise NOT | Inverts the bits (complement operation) |
<< |
Left Shift | Shifts bits to the left, filling with zeros |
>> |
Right Shift | Shifts bits to the right, preserving the sign bit |
>>> |
Unsigned Right Shift | Shifts bits to the right, filling with zeros |
3. Bitwise AND (&) with Examples
The bitwise AND (&) operator compares corresponding bits of two numbers and returns 1 if both bits are 1; otherwise, it returns 0.
Example 1: Bitwise AND
public class Main { public static void main(String[] args) { int a = 6; // Binary: 110 int b = 3; // Binary: 011 int result = a & b; // Binary: 010 (decimal 2) System.out.println("Bitwise AND: " + result); // Output: 2 } }
Explanation:
6 in binary is 110, and 3 in binary is 011.
The bitwise AND operation results in 010, which is 2 in decimal.
4. Bitwise OR (|) with Examples
The bitwise OR (|) operator compares corresponding bits of two numbers and returns 1 if at least one of the bits is 1; otherwise, it returns 0.
Example 2: Bitwise OR
public class Main { public static void main(String[] args) { int a = 6; // Binary: 110 int b = 3; // Binary: 011 int result = a | b; // Binary: 111 (decimal 7) System.out.println("Bitwise OR: " + result); // Output: 7 } }
Explanation:
The OR operation between 110 (6) and 011 (3) results in 111, which is 7 in decimal.
5. Bitwise XOR (^) with Examples
The bitwise XOR (^) operator returns 1 if the corresponding bits of two numbers are different; otherwise, it returns 0.
Example 3: Bitwise XOR
public class Main { public static void main(String[] args) { int a = 6; // Binary: 110 int b = 3; // Binary: 011 int result = a ^ b; // Binary: 101 (decimal 5) System.out.println("Bitwise XOR: " + result); // Output: 5 } }
Explanation:
XOR between 110 (6) and 011 (3) results in 101, which is 5 in decimal.
6. Bitwise NOT (~) with Examples
The bitwise NOT (~) operator inverts all the bits of the number, turning 1s into 0s and vice versa. This operation is also called the bitwise complement.
Example 4: Bitwise NOT
public class Main { public static void main(String[] args) { int a = 5; // Binary: 00000000 00000000 00000000 00000101 int result = ~a; // Inverted: 11111111 11111111 11111111 11111010 (decimal -6) System.out.println("Bitwise NOT: " + result); // Output: -6 } }
Explanation:
The bitwise NOT of 5 flips every bit. In 32-bit twoโs complement representation, ~5 is -6.
7. Bitwise Shift Operators
Left Shift (<<)
The left shift (<<) operator shifts all bits of the number to the left by a specified number of positions, filling the right side with zeros. This is equivalent to multiplying the number by 2^n, where n is the number of positions shifted.
Example 5: Left Shift
public class Main { public static void main(String[] args) { int a = 3; // Binary: 11 int result = a << 2; // Shifts left by 2: 1100 (decimal 12) System.out.println("Left Shift: " + result); // Output: 12 } }
Explanation:
Shifting 3 left by 2 positions gives 12 (1100 in binary).
Right Shift (>>)
The right shift (>>) operator shifts all bits of the number to the right by a specified number of positions, preserving the sign bit (leftmost bit). This is equivalent to dividing the number by 2^n.
Example 6: Right Shift
public class Main { public static void main(String[] args) { int a = 12; // Binary: 1100 int result = a >> 2; // Shifts right by 2: 11 (decimal 3) System.out.println("Right Shift: " + result); // Output: 3 } }
Explanation:
Shifting 12 right by 2 positions gives 3 (11 in binary).
Unsigned Right Shift (>>>)
The unsigned right shift (>>>) operator shifts all bits to the right and fills the leftmost bits with zeros, regardless of the sign bit. This operator works similarly to the right shift, but it does not preserve the sign.
Example 7: Unsigned Right Shift
public class Main { public static void main(String[] args) { int a = -12; // Binary: 11111111 11111111 11111111 11110100 (two's complement) int result = a >>> 2; // Shift right by 2, filling with zeros System.out.println("Unsigned Right Shift: " + result); // Output: 1073741821 } }
Explanation:
The unsigned right shift shifts -12 to the right by 2 positions and fills the leftmost bits with 0s, resulting in a large positive number.
8. Working with Bit Masks
A bit mask is a value used to select specific bits from another value using bitwise operators. Bit masks are useful when you need to manipulate specific bits.
Example 8: Using Bit Mask to Set Specific Bits
public class Main { public static void main(String[] args) { int value = 42; // Binary: 101010 int mask = 1 << 3; // Bit mask to set the 3rd bit: 1000 int result = value | mask; // Set the 3rd bit: 101010 | 1000 = 101010 (remains 42) System.out.println("Result with Bit Mask: " + result); } }
Explanation:
The bit mask 1 << 3 shifts a 1 to the 3rd position, and the OR operation sets the 3rd bit of the original value.
9. Bitwise Operators on Different Data Types
Bitwise operators in Java work on int, byte, short, char, and long. If you are working with smaller types like byte or short, the values are automatically promoted to int before the bitwise operation, and the result is an int.
Example 9: Bitwise Operation on byte
public class Main { public static void main(String[] args) { byte a = 9; // Binary: 1001 byte b = 12; // Binary: 1100 int result = a & b; // Binary: 1000 (decimal 8) System.out.println("Bitwise AND on byte: " + result); // Output: 8 } }
10. Common Use Cases for Bitwise Operators
Example 10: Checking if a Number is Even or Odd Using Bitwise AND
public class Main { public static void main(String[] args) { int number = 13; if ((number & 1) == 0) { System.out.println(number + " is even."); } else { System.out.println(number + " is odd."); } } }
Explanation:
The least significant bit (LSB) of an odd number is always 1, so number & 1 checks if the LSB is 1 (odd) or 0 (even).
Example 11: Swapping Two Numbers Using XOR
public class Main { public static void main(String[] args) { int a = 5, b = 9; a = a ^ b; // Step 1 b = a ^ b; // Step 2 a = a ^ b; // Step 3 System.out.println("Swapped values: a = " + a + ", b = " + b); // Output: a = 9, b = 5 } }
Explanation:
XOR swapping is a technique that swaps two values without using a temporary variable.
Conclusion
Java's bitwise operators allow you to manipulate individual bits within integer types, which is useful in various low-level programming tasks, such as bit manipulation, data encoding, and working with hardware or protocols.
By understanding operators like AND (&), OR (|), XOR (^), NOT (~), and bit shifts, you can efficiently perform bit-level operations.
Through the examples in this tutorial, you should now have a better understanding of how to use bitwise operators in Java.