Sub topic 14
Signed and Unsigned Shifts
A shift moves the bits of an integer left or right. A bit is a single 0 or 1 used to store a value. Shifts are useful when working with flags, packed data, or values read from a file or device.
This chapter assumes you know Java variables, integer types, and basic bitwise operations. You should also know how to run a simple Java program.
How shifts work
A shift expression has a value on the left and a shift distance on the right. The shift distance tells Java how many bit positions to move.
For example, 10 << 1 moves the bits of 10 one place to the left. The result is 20.
Java has three shift operators:
| Operator | Name | What fills the empty positions? |
|---|---|---|
| `<<` | Left shift | Zeros on the right |
| `>>` | Signed right shift | Copies of the sign bit on the left |
| `>>>` | Unsigned right shift | Zeros on the left |
An int has 32 bits, and a long has 64 bits. The sign bit is the leftmost bit. For these signed types, a sign bit of 0 indicates a nonnegative value, and a sign bit of 1 indicates a negative value.
Shift left with `<<`
A left shift moves bits toward the left. Bits that move past the left edge are lost. Zeros enter from the right.
The binary form of 10 ends in 1010. The prefix 0b marks a binary value in Java.
| Expression | Ending bits | Result |
|---|---|---|
| `0b1010` | `1010` | `10` |
| `0b1010 << 1` | `10100` | `20` |
| `0b1010 << 2` | `101000` | `40` |
When no important bits are lost, shifting left by one is like multiplying by two. Shifting left by two is like multiplying by four.
That shortcut does not always work. A shift can lose bits or change the sign bit. This is a form of overflow: the intended value does not fit in the type. Java does not report an error when this happens.
For example, 1 << 31 produces -2147483648, not a positive number. The 1 has moved into the sign-bit position of an int.
Shift right with `>>` and `>>>`
Both right-shift operators discard bits that move past the right edge. They differ in what they put on the left.
The signed right shift, >>, copies the original sign bit. This keeps a negative value negative.
The unsigned right shift, >>>, always inserts zeros. For a negative value and a nonzero effective shift distance, this produces a nonnegative result.
Here is what happens to -8 in a 32-bit int. The spaces group the bits so they are easier to read.
| Expression | 32-bit pattern | Result |
|---|---|---|
| `-8` | `11111111 11111111 11111111 11111000` | `-8` |
| `-8 >> 1` | `11111111 11111111 11111111 11111100` | `-4` |
| `-8 >>> 1` | `01111111 11111111 11111111 11111100` | `2147483644` |
In the second row, >> fills the new leftmost position with 1. In the third row, >>> fills it with 0. That one difference changes the sign and the result.
For a nonnegative value, the two operators give the same result for the same shift distance. For example, both 8 >> 1 and 8 >>> 1 produce 4.
Right shift is not always division
For positive integers, >> 1 gives the same result as integer division by two. For negative odd integers, the results can differ:
-3 >> 1is-2.-3 / 2is-1.
Integer division drops the fractional part toward zero. Signed right shift behaves like division rounded down. Choose the operation that matches the result you need.
Run a shift example
Save this program as ShiftDemo.java.
public class ShiftDemo {
public static void main(String[] args) {
int value = 10;
System.out.println(value << 1); // 20
System.out.println(value >> 1); // 5
System.out.println(value >>> 1); // 5
int negative = -8;
System.out.println(negative >> 1); // -4
System.out.println(negative >>> 1); // 2147483644
System.out.println(-3 >> 1); // -2
System.out.println(-3 / 2); // -1
System.out.println(1 << 31); // -2147483648
System.out.println(1 << 32); // 1
System.out.println(1L << 32); // 4294967296
}
}Compile and run it from the directory containing the file:
javac ShiftDemo.java
java ShiftDemoThe L in 1L makes that value a long. The last two lines show why the type of the left operand matters: 1 << 32 is an int shift, while 1L << 32 is a long shift.
Shift distances have limits
Java does not simply use every shift distance as written. It uses an effective shift distance:
- For an
int, the effective distance is from0to31. - For a
long, the effective distance is from0to63.
For a nonnegative distance, this works like taking the remainder after division by 32 for an int, or by 64 for a long.
| Expression | Effective distance | Result |
|---|---|---|
| `1 << 32` | `0` | `1` |
| `1 << 33` | `1` | `2` |
| `1L << 32` | `32` | `4294967296` |
| `1L << 64` | `0` | `1` |
Java applies this rule to negative distances too. For example, 1 << -1 has an effective distance of 31, so its result is -2147483648. Avoid negative distances: they are easy to misread.
A zero effective distance leaves the bits unchanged. In particular, -1 >>> 32 is still -1, because the effective distance for that int shift is zero.
Smaller integer types are promoted
A byte has 8 bits. Before a shift, Java promotes a byte, short, or char left operand to an int. Promotion means Java uses the value as a wider type for the operation. The result of such a shift is an int.
This matters especially for >>> on a negative byte. Try the following complete program:
public class ByteShiftDemo {
public static void main(String[] args) {
byte data = (byte) 0b10000000; // -128
int shifted = data >>> 1;
int cleared = (data & 0b11111111) >>> 1;
System.out.println(shifted); // 2147483584
System.out.println(cleared); // 64
}
}When Java promotes data, its negative value is represented as a negative 32-bit int. The >>> operator then shifts that 32-bit pattern, not just the original eight bits.
The expression data & 0b11111111 keeps the lowest eight bits and clears the higher ones. Here, & is bitwise AND: it keeps a 1 only where both inputs have a 1. The cleared value is 128, so shifting it right by one gives 64.
Choose a shift
- Use
<<to move bits left, but check whether losing high bits is acceptable. - Use
>>when shifting a signed value and you want to preserve its sign. - Use
>>>when you want zeros to enter on the left, such as when reading fields from a bit pattern. - Use ordinary multiplication or division when the goal is arithmetic rather than moving bits. It is often easier to read.
A common bit-reading expression is (value >>> n) & 1. It moves bit n to the rightmost position, then keeps only that bit. The result is 0 or 1. For an int, check that n is between 0 and 31.
Practice
Exercise
What are the results of 12 << 1 and 12 >> 1?
Show answer
They are 24 and 6. The first shifts left by one; the second shifts right by one.
Exercise
Why does -8 >>> 1 produce a positive result while -8 >> 1 stays negative?
Show answer
>>> fills the leftmost position with 0. >> copies the original sign bit, which is 1 for -8.
Exercise
What is the result of 1 << 33, and why?
Show answer
It is 2. For an int, 33 has an effective shift distance of 1.
Exercise
What are the results of -3 >> 1 and -3 / 2?
Show answer
They are -2 and -1. The right shift rounds down, while integer division drops the fractional part toward zero.