Understanding Bitwise Operations in Java: Uses, Representations, and the Seven Operators
This article explains the purpose and practical benefits of bitwise operations in Java, describes signed number representations (sign‑magnitude, ones' complement, two's complement), shows how bits can optimize time and space, and details the seven common bitwise operators with examples and a reference table.
Computers store all numbers as binary, and bitwise operations manipulate these bits directly, making them a fundamental skill for developers.
Why Use Bitwise Operations?
In production code they are used to optimize time and space . While they do not change algorithmic complexity, they can reduce the constant factor dramatically.
For example, a binary heap (a complete binary tree) has insertion and deletion costs of O(logn) . A Fibonacci heap can achieve O(1) insertion and O(logn) deletion, but the large constant factor of the O(1) operation often makes the classic heap faster in practice.
When the data size fits within an int (32 bits), a single integer can represent up to 32 boolean flags, allowing a compact representation of feature support such as browser compatibility.
Signed Number Representations
Java uses signed types . The most significant bit (MSB) is the sign bit: 0 for non‑negative numbers, 1 for negative numbers.
Positive Numbers
For positive values, the original code, one's complement, and two's complement are identical.
int 1 = 0b0000 0000 0000 0001
int 2 = 0b0000 0000 0000 0010
Negative Numbers
Original code : set the sign bit to 1 while keeping the magnitude bits unchanged.
-1 original = 0b1000 0000 0000 0001
-2 original = 0b1000 0000 0000 0010
One's complement (invert all bits except the sign bit):
-1 ones' complement = 0b1111 1111 1111 1110
-2 ones' complement = 0b1111 1111 1111 1101
Two's complement (one's complement + 1) is the representation actually stored in memory:
-1 two's complement = 0b1111 1111 1111 1111
-2 two's complement = 0b1111 1111 1111 1110
Using two's complement allows addition and subtraction to be performed without separate sign handling, simplifying CPU design.
The maximum value of a signed 32‑bit int is 01111111111111111111111111111111 (31 ones) = 2 31 − 1 = 2147483647.
Seven Common Bitwise Operators
Operator
Chinese
English
Rule
<<
左移
left shift
Shift left, fill right with 0
>>
右移
signed right shift
Shift right, fill left with sign bit
>>>
无符号右移
unsigned right shift
Java‑specific, fill left with 0
~
位非
NOT
Invert each bit
&
位与
bitwise AND
Bitwise conjunction (1 only if both bits are 1)
|
位或
OR
Bitwise disjunction (1 if any bit is 1)
^
异或
XOR
Bitwise exclusive OR (1 if bits differ)
The first four operators act on a single operand and leave the original value unchanged; the last three combine two operands.
1. Left Shift (<<)
Shifts bits left by *n* positions, filling the right side with 0. Effectively multiplies a non‑overflowing positive number by 2 n .
1 = 0b0000 0001
1 << 1 = 0b0000 0010 = 2
2 = 0b0000 0010
2 << 1 = 0b0000 0100 = 4
3 = 0b0000 0011
3 << 1 = 0b0000 0110 = 6
2. Signed Right Shift (>>)
Shifts bits right, filling the left side with the original sign bit. For non‑negative numbers this divides by 2, but for negative numbers the result rounds toward zero.
1 >> 1 = 0b0000 0000 = 0
2 >> 1 = 0b0000 0001 = 1
3 >> 1 = 0b0000 0001 = 1
-3 = 1111 1101
-3 >> 1 = 1111 1110 = -2
For even negative numbers the result equals division by 2; for odd negatives it differs because Java truncates toward zero.
3. Unsigned Right Shift (>>>)
Similar to signed right shift but always fills the left side with 0, making it useful for treating the value as unsigned.
4. Bitwise NOT (~)
Inverts every bit: 0 becomes 1 and 1 becomes 0.
3 = 0b0000 0011
~3 = 0b1111 1100
5. Bitwise AND (&)
Result bit is 1 only if both corresponding bits are 1.
3 = 0b0000 0011
5 = 0b0000 0101
3 & 5 = 0b0000 0001
6. Bitwise OR (|)
Result bit is 1 if at least one of the corresponding bits is 1.
3 = 0b0000 0011
5 = 0b0000 0101
3 | 5 = 0b0000 0111
7. Bitwise XOR (^)
Result bit is 1 if the corresponding bits differ.
3 = 0b0000 0011
5 = 0b0000 0101
3 ^ 5 = 0b0000 0110
These operators form the basis for many advanced techniques in networking protocols, operating systems, and performance‑critical code.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.