1. Java-supported bit operators
Java provides 7 bit operators:
Operators | name | describe | Example |
---|---|---|---|
& |
Bitwise and (AND) | When both bits are 1, the result is 1 |
5 & 3 → 1
|
| |
bitwise or (OR) | When one of the two bits is 1, the result is 1 |
5 | 3 → 7
|
^ |
Bitwise XOR (XOR) | The two bits are different, the result is 1 |
5 ^ 3 → 6
|
~ |
Negative bitwise (NOT) | All bits are inverted (0→1, 1→0) |
~5 → -6
|
<< |
Left Shift | All bits move to the left, and the low bit is filled 0 |
5 << 1 → 10
|
>> |
Right Shift | All bits move to the right, and the high bits complement the sign bits |
-5 >> 1 → -3
|
>>> |
Unsigned Right Shift | All bits move to the right, and the high bits fill 0 |
-5 >>> 1 → 2147483645
|
2. Detailed explanation of bit operations
(1) Bitwise and (&)
rule: When both bits are 1, the result is 1, otherwise it is 0.
-
Example:
int a = 5; // Binary: 0101int b = 3; // Binary: 0011int c = a & b; // Binary: 0001 → 1(c); // Output:1
-
application:
Judge odd and even:
(n & 1) == 0
→ Even number.
(2) bitwise or (|)
rule: When one of the two bits is 1, the result is 1.
-
Example:
int a = 5; // 0101 int b = 3; // 0011 int c = a | b; // 0111 → 7 (c); // Output:7
-
application:
Merge multiple flag bits (such as
READ | WRITE | EXECUTE
)。
(3) Bitwise XOR (^)
rule: The two bits are different, the result is 1, otherwise it is 0.
-
Example:
int a = 5; // 0101 int b = 3; // 0011 int c = a ^ b; // 0110 → 6 (c); // Output:6
-
characteristic:
a ^ a = 0
(The same number is XOR 0).a ^ 0 = a
(Any number is exclusive or unchanged with 0).
-
application:
-
Exchange two numbers (no temporary variables):
int x = 5, y = 3; x = x ^ y; y = x ^ y; // y = (x ^ y) ^ y = x x = x ^ y; // x = (x ^ y) ^ x = y (x + ", " + y); // Output:3, 5
-
(4) Bitwise invert (~)
rule: Inverse all bits (0→1, 1→0).
-
Example:
int a = 5; // 0000 0101 int b = ~a; // 1111 1010 (complement code representation)(b); // Output:-6
-
Notice:
Java uses complement to represent negative numbers.
~5
Actually it is-6
。
(5) Move left (<<)
rule: All bits move to the left, and the low bit is filled with 0.
-
Example:
int a = 5; // 0000 0101 int b = a << 1; // 0000 1010 → 10 (b); // Output:10
-
application:
Quick calculation
a * 2^n
(likea << 3
=a * 8
)。
(6) Move right (>>)
rule: All bits move to the right, and the high bit complements the sign bits (positive number is 0, negative number is 1).
-
Example:
int a = 5; // 0000 0101 int b = a >> 1; // 0000 0010 → 2 int c = -5 >> 1; // 1111 1101 → -3 (b + ", " + c); // Output:2, -3
-
application:
Quick calculation
a / 2^n
(likea >> 2
=a / 4
)。
(7) Unsigned right (>>>)
rule: All bits move to the right, and the high bit is supplemented by 0 (not considering the sign bit).
-
Example:
int a = -5; // 1111 1111 1111 1111 1111 1111 1111 1011 int b = a >>> 1; // 0111 1111 1111 1111 1111 1111 1111 1101 → 2147483645 (b); // Output:2147483645
-
application:
Handle unsigned integers (Java does not have unsigned types, but can be used
>>>
Simulation).
3. Common uses of bit operations
(1) Judgment odd and even
if ((n & 1) == 0) { ("even"); } else { ("odd number"); }
(2) Exchange two numbers
int x = 5, y = 3; x = x ^ y; y = x ^ y; x = x ^ y; (x + ", " + y); // Output:3, 5
(3) Calculate the absolute value
int a = -5; int abs = (a ^ (a >> 31)) - (a >> 31); (abs); // Output:5
(4) Determine whether it is a power of 2
boolean isPowerOfTwo = (n & (n - 1)) == 0; (isPowerOfTwo); // 8 → true, 7 → false
(5) Quick calculation 2^n
int power = 1 << n; // 2^n (power); // n=3 → 8
4. Summary
Operators | use | Example |
---|---|---|
& |
Judgment of odd and even, permission control | (n & 1) == 0 |
| |
Merge flag bits | READ | WRITE |
^ |
Exchange variables, encryption | a ^= b; b ^= a; a ^= b; |
~ |
Reverse | ~5 → -6 |
<< |
Quickly multiply by 2^n | 5 << 2 → 20 |
>> |
Quickly divide by 2^n | -5 >> 1 → -3 |
>>> |
Unsigned right | -5 >>> 1 → 2147483645 |
This is the end of this article about the bit operators of common Java operators. For more information about Java bit operators, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!