Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Java Right Shift: Signed and Unsigned

Understand Java's signed (>>) and unsigned (>>>) right shift operators, how they treat negative numbers, and when to use each for bit manipulation.

~5 min readJava
#bit manipulation#signed right shift#unsigned right shift#Java operators
Illustration of Java integer bit shift showing signed and unsigned right shift operations with binary patterns.

Java's right shift operators seem simple at first glance: both >> and >>> move bits to the right. The important difference is how they handle the sign bit when the value is negative. >> is the signed right shift: it preserves the sign by filling the high-order bits with 1s for negative numbers. >>> is the unsigned right shift: it always fills those bits with 0s. This distinction matters for bit manipulation, binary protocols, and any code that depends on the exact bit representation of an integer.

The Two Operators: >> and >>>

Java provides two right shift operators:

OperatorNameBehavior for negative numbers
>>Signed right shiftPreserves the sign bit, filling with 1s for negative numbers
>>>Unsigned right shiftFills with 0s, regardless of sign

Both operators work on int and long types. For char, byte, and short, the value is first promoted to int before shifting; the result type is that promoted type.

The signed right shift >> is equivalent to dividing by two to the power of the shift amount, with rounding toward negative infinity. The unsigned right shift >>> treats the value as an unsigned bit pattern, which is useful for bit-level operations that do not care about the sign.

Signed Right Shift (>>) with Negative Numbers

When you apply >> to a negative number, the vacant high-order bits are filled with 1, preserving the negative sign of the original value. Consider this example with int:

java
int negative = -8; // binary: 11111111 11111111 11111111 11111000 int shifted = negative >> 1; // result: -4

The two's-complement representation of -8 starts with many 1s. Shifting right by one fills the leftmost bit with a 1, resulting in -4. This is the behavior of an arithmetic right shift.

The signed shift is often used for division by powers of two, but be careful: >> truncates toward negative infinity, which differs from integer division that truncates toward zero. For example, -7 / 2 in Java yields -3 (truncation toward zero), but -7 >> 1 yields -4 (floor division).

Unsigned Right Shift (>>>) Uses Zero-Fill

The unsigned right shift >>> always fills the leftmost bits with zero. This is the logical right shift. It does not care about the sign; it simply moves bits right, and the highest bits become 0. For example:

java
int negative = -8; int shifted = negative >>> 1; // result: 2147483644

Here the 32-bit representation of -8 becomes 01111111 11111111 11111111 11111100, which is 2147483644 in decimal. This can be surprising if you expected the sign to be preserved.

Unsigned right shift is particularly useful when dealing with raw binary data, such as encoding or decoding byte arrays, or when you need to treat an int as a sequence of bits independent of its numeric meaning.

Practical Example: Packing Bytes Into an int

A common real-world usage for >>> is packing multiple small values into a single int and reading them back. Here, four byte-sized values are packed into a 32-bit int. To extract each byte, shift right and mask, and use >>> to avoid sign extension when the packed int is negative.

java
public class BytePacking { public static void main(String[] args) { int a = 0xDE; // 222 int b = 0xAD; // 173 int c = 0xBE; // 190 int d = 0xEF; // 239 int packed = (a << 24) | (b << 16) | (c << 8) | d; int extractedA = (packed >>> 24) & 0xFF; int extractedB = (packed >>> 16) & 0xFF; int extractedC = (packed >>> 8) & 0xFF; int extractedD = packed & 0xFF; System.out.println(extractedA + " " + extractedB + " " + extractedC + " " + extractedD); } }

Because a's high bit is set, packed is negative in two's-complement representation. For the highest byte, the raw result of packed >> 24 would be 0xFFFFFFDE; packed >>> 24 returns 0x000000DE directly. For the lower bytes, the mask & 0xFF selects the 8 bits you want.

Behavior with byte and short

When you shift a byte or short, Java first promotes the value to int. This can cause unexpected results if you assume the shift operates directly on the original type.

java
byte b = -1; // binary for byte: 11111111 int result = b >> 1;

Because b is promoted to int, the int value is 0xFFFFFFFF, and the right shift results in 0xFFFFFFFF, which is -1. If you wanted the byte to be treated as unsigned and shift out the sign, you need to mask it first:

java
byte b = -1; int unsigned = (b & 0xFF) >> 1; // result: 127

This is a common source of bugs in code that handles binary protocols or image data.

Choosing Between >> and >>>

The choice between signed and unsigned shift depends entirely on what the bits represent. If the value is a signed integer and you need division semantics, >> is appropriate. If you are working with raw bits, flags, or packed data, >>> is likely correct.

Use caseOperator
Division by power of two for signed integers>>
Handling binary protocols, bit flags, hash functions>>>
Shifting non-negative integers where sign is irrelevantBoth work

The signed shift >> can be used for positive numbers just like >>>, but when the value is negative, the result differs. Always consider whether the sign bit should influence the result.

Performance and Maintainability Considerations

From a correctness standpoint, the operator choice is far more important than runtime performance. In normal code, any performance difference between >> and >>> is not a practical reason to choose one over the other. The real risk is using the wrong operator and silently corrupting data when negative values are involved.

Code that appears correct for positive inputs may break once the input becomes negative. Therefore, it is wise to:

  • Document the intended meaning of the bit field.
  • Prefer >>> when treating the value as a bit pattern.
  • Use >> only when the numeric signed value is the focus.

This prevents subtle bugs in production systems where data is packed and unpacked.

Edge Cases and Pitfalls

Shift distance in Java is masked to the lower bits. For int, the shift distance is masked to five bits, so the effective distance is 0 to 31. Shifting by 32 is equivalent to shifting by 0. This is a known Java pitfall.

java
int value = 1; int result = value >> 32; // result is 1, not 0

This happens because Java uses only the five lowest-order bits of the shift distance for int shifts, and 32 & 0x1F is 0. For long, Java uses only the six lowest-order bits (mask 0x3F). This behavior is part of the Java language specification and can lead to hard-to-spot bugs if you assume a shift by the type's bit width clears the value.

Another edge case is shifting by a negative distance. Java uses the same masked lower bits, so x >> -1 is equivalent to x >> 31 for an int. If shift distances come from external input, validate them explicitly.

Choose the right operator based on what the bits represent: use >>> for bit patterns and packed data to avoid sign extension, and reserve >> for signed arithmetic. Remember the shift-distance masking behavior and promote smaller types carefully to avoid subtle data corruption.

Try matching browser tools

All tools

Small utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Java guides

More in Java

All Java guides

The guides filed next to this one in Java, in library order.