Quick revision
The short answer
The >> operator is an arithmetic shift: it moves the bits to the right and copies the sign bit into the empty places, so negative numbers stay negative. The >>> operator is a logical shift that always fills with zeros. So -8 >> 1 is -4, -8 >>> 28 is 15, 1 << 33 for an int is 2 because the distance is masked to 5 bits, and 1L << 33 is 8589934592 because a long uses 6 bits of the distance.
What to remember
In this answer, we saw that >> copies the sign bit while >>> fills with zeros, which is why -8 >> 1 is -4 and -8 >>> 28 is 15. We saw that the shift distance is masked to 5 bits for int and 6 bits for long, so 1 << 33 is 2 and 1L << 33 is 8589934592, and that byte and short are promoted to int before shifting, which makes byte >>>= 1 give a surprising -4. Finally, we saw how HashMap and binary search rely on the unsigned shift.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The >> operator is an arithmetic shift: it moves the bits to the right and copies the sign bit into the empty places, so negative numbers stay negative. The >>> operator is a logical shift that always fills with zeros. So -8 >> 1 is -4, -8 >>> 28 is 15, 1 << 33 for an int is 2 because the distance is masked to 5 bits, and 1L << 33 is 8589934592 because a long uses 6 bits of the distance.
Why this question is asked
Shift operators look like a topic for low-level code, but we meet them in hash functions, in bit flags, in binary protocols and in every place that packs data into an int or a long. Interviewers ask this exact set of expressions because each one hides a rule that catches people out: sign extension, the zero fill, and the masking of the shift distance. In this answer, we will recap how an int is stored in two's complement, work through each expression bit by bit, look at the masking rule that makes 1 << 33 equal 2, and finish with the promotion of byte and short, which produces one of the most surprising results in Java.
1A Quick Recap of Two's Complement
An int in Java is 32 bits and a long is 64 bits. Both are signed, and both use two's complement, the standard way of storing negative numbers. In two's complement the leftmost bit, the sign bit, is 1 for negative numbers and 0 for positive ones. To get the pattern of a negative number we take the positive value, flip every bit and add one.
For 8 the pattern is 0000 ... 0000 1000. Flipping gives 1111 ... 1111 0111, and adding one gives 1111 ... 1111 1000, which is 0xFFFFFFF8 in hexadecimal. That is -8. Notice that almost all the bits of a small negative number are ones. This is why shifting it to the right gives such different results depending on what we fill the empty places with.
2Working Through -8 >> 1 and -8 >>> 28
A right shift moves every bit to the right and drops the bits that fall off the end. The question is what goes into the empty places on the left. With >>, Java copies the sign bit into them. This is called sign extension, and it is what makes >> behave like division by a power of two, rounded towards negative infinity. With >>>, Java puts zeros into them, whatever the sign was. This treats the 32 bits as an unsigned number.
| Expression | Bit pattern (32 bits) | Value |
|---|---|---|
| -8 | 1111 1111 1111 1111 1111 1111 1111 1000 | -8 |
| -8 >> 1 | 1111 1111 1111 1111 1111 1111 1111 1100 | -4 |
| -8 >>> 1 | 0111 1111 1111 1111 1111 1111 1111 1100 | 2147483644 |
| -8 >>> 28 | 0000 0000 0000 0000 0000 0000 0000 1111 | 15 |
For -8 >> 1, the pattern 1111 ... 1000 moves right by one and a 1 comes in on the left, giving 1111 ... 1100, which is -4. For -8 >>> 28, we move right by 28 places and zeros come in. Only the top four bits of the original survive, and they were 1111, so the result is 0000 ... 1111, which is 15.
3Why 1 << 33 Is 2 for an int
Now the left shift. 1 << n moves the single 1 bit up by n places, so it normally gives 2 to the power n. We might expect 1 << 33 to overflow to 0, because the bit moves past the top of a 32-bit int. It does not. The Java Language Specification says that for an int only the lowest 5 bits of the shift distance are used, which is the same as taking the distance modulo 32. 33 is 100001 in binary, its lowest 5 bits are 00001, so 1 << 33 is really 1 << 1, which is 2.
For a long, Java uses the lowest 6 bits of the distance, which is the distance modulo 64. So 1L << 33 really shifts by 33 and gives 8589934592, which is 2 to the power 33. The rule comes from the processors Java runs on: x86 shift instructions mask the distance the same way, so the language can compile a shift to one instruction with no extra checks. The cost is that a shift by a computed distance never fails loudly. If a distance can reach 32 or more, we should either use a long or check the range ourselves.
4The Promotion Trap With byte and short
There is one more rule. Before any shift, a byte, short or char operand is promoted to int. So the shift always happens on at least 32 bits, and the result is an int. Most of the time this is invisible, but it bites with negative bytes and the compound form >>>=.
Notice what happened with b >>>= 1. The byte -8 became the int 0xFFFFFFF8, the logical shift gave 0x7FFFFFFC, and the implicit cast back to byte kept only the lowest 8 bits, 0xFC, which is -4 again. The zero we hoped to shift in landed far to the left and was thrown away. The fix is to mask the byte to its unsigned value with & 0xFF before shifting, which is the standard idiom when reading binary data.
5Where We Meet >>> in Real Code
The most famous use is inside HashMap. Before choosing a bucket, HashMap computes h ^ (h >>> 16) from the key's hashCode(). The bucket index is taken from the lowest bits only, so without this step keys whose hash codes differ only in their upper bits would all land in the same bucket. Shifting the upper half down and combining it with XOR mixes those bits into the part that is used. The unsigned shift is the natural choice here: it brings the top 16 bits down unchanged and fills the top with zeros, so the upper half of the result keeps the original upper bits.
We also use >>> for an overflow-safe midpoint in binary search, (low + high) >>> 1, which gives the right answer even when low + high overflows past Integer.MAX_VALUE, because the unsigned shift reads the overflowed sum as the large positive number it really is.
6Follow-up questions
How does HashMap use h ^ (h >>> 16) when spreading hash codes?
HashMap picks the bucket with (n - 1) & hash, which only looks at the lowest bits when the table is small. h >>> 16 moves the upper 16 bits into the lower half and fills with zeros, and the XOR combines them with the original lower bits. Keys that differ only in their upper bits, such as some Float or Long keys, then land in different buckets. It is a cheap step, one shift and one XOR, that protects the map from badly distributed hash codes.
Is x >> 1 always the same as x / 2?
Only for non-negative numbers. For negative odd numbers, >> rounds towards negative infinity and / rounds towards zero, so -7 >> 1 is -4 and -7 / 2 is -3. The JIT compiler already turns division by a constant power of two into fast shifts with a correction, so writing the shift by hand rarely buys anything.
Why does Java mask the shift distance instead of throwing an error?
Because the hardware does the same thing, so a shift compiles to a single instruction without a range check, and the behaviour is defined on every platform. In C, shifting by the width of the type or more is undefined behaviour; Java chose a defined but surprising result instead.
7Conclusion
In this answer, we saw that >> copies the sign bit while >>> fills with zeros, which is why -8 >> 1 is -4 and -8 >>> 28 is 15. We saw that the shift distance is masked to 5 bits for int and 6 bits for long, so 1 << 33 is 2 and 1L << 33 is 8589934592, and that byte and short are promoted to int before shifting, which makes byte >>>= 1 give a surprising -4. Finally, we saw how HashMap and binary search rely on the unsigned shift.
Open with
The difference is what fills the empty bits on the left. The >> operator is an arithmetic shift, it copies the sign bit, so a negative number stays negative and it acts like division by a power of two that rounds down.
Then, if they want more
The >>> operator is a logical shift, it always fills with zeros. So -8, which is all ones ending in 1000, shifted right by one with >> gives -4, while -8 >>> 28 keeps only the top four bits, which were ones, and gives 15.
For the left shifts, Java masks the shift distance: for an int it uses only the low 5 bits, so 1 << 33 is really 1 << 1, which is 2, and for a long it uses 6 bits, so 1L << 33 is 8589934592.
I would also mention that byte, short and char are promoted to int before any shift, which is why a negative byte shifted with >>>= comes back still negative unless I mask it with 0xFF first.
A real place this matters is HashMap, which spreads hash codes with h XOR h >>> 16 so the high bits influence the bucket index.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain >> versus >>> and what these give: -8 >> 1, -8 >>> 28, 1 << 33 for an int, and...": https://scrivio-interview-library.netlify.app/java/operators-and-control-flow/9118154729/