What is the difference between the operators >>>
and >>
in Java?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
>>>
will always put a 0 in the left most bit, while>>
will put a 1 or a 0 depending on what the sign of it is.>>
is arithmetic shift right,>>>
is logical shift right.In an arithmetic shift, the sign bit is extended to preserve the signedness of the number.
For example: -2 represented in 8 bits would be
11111110
(because the most significant bit has negative weight). Shifting it right one bit using arithmetic shift would give you11111111
, or -1. Logical right shift, however, does not care that the value could possibly represent a signed number; it simply moves everything to the right and fills in from the left with 0s. Shifting our -2 right one bit using logical shift would give01111111
.Read more about Bitwise and Bit Shift Operators
The bit pattern is given by the left-hand operand, and the number of positions to shift by the right-hand operand. The unsigned right shift operator
>>>
shifts a zero into the leftmost position,while the leftmost position after
>>
depends on sign extension.In simple words
>>>
always shifts a zero into the leftmost position whereas>>
shifts based on sign of the number i.e. 1 for negative number and 0 for positive number.For example try with negative as well as positive numbers.
output:
The logical right shift (
v >>> n
) returns a value in which the bits inv
have been shifted to the right byn
bit positions, and 0's are shifted in from the left side. Consider shifting 8-bit values, written in binary:If we interpret the bits as an unsigned nonnegative integer, the logical right shift has the effect of dividing the number by the corresponding power of 2. However, if the number is in two's-complement representation, logical right shift does not correctly divide negative numbers. For example, the second right shift above shifts 128 to 32 when the bits are interpreted as unsigned numbers. But it shifts -128 to 32 when, as is typical in Java, the bits are interpreted in two's complement.
Therefore, if you are shifting in order to divide by a power of two, you want the arithmetic right shift (
v >> n
). It returns a value in which the bits inv
have been shifted to the right byn
bit positions, and copies of the leftmost bit of v are shifted in from the left side:When the bits are a number in two's-complement representation, arithmetic right shift has the effect of dividing by a power of two. This works because the leftmost bit is the sign bit. Dividing by a power of two must keep the sign the same.
>>>
is unsigned-shift; it'll insert 0.>>
is signed, and will extend the sign bit.JLS 15.19 Shift Operators
To make things more clear adding positive counterpart
Since it is positive both signed and unsigned shifts will add 0 to left most bit.
Related questions
1 >>> 32 == 1
The right shift logical operator (
>>> N
) shifts bits to the right by N positions, discarding the sign bit and padding the N left-most bits with 0's. For example:after a
>>> 1
operation becomes:The right shift arithmetic operator (
>> N
) also shifts bits to the right by N positions, but preserves the sign bit and pads the N left-most bits with 1's. For example:after a
>> 1
operation becomes: