What is difference between >> and >>> operators?
What is difference between >> and >>> operators?
The >>
and >>>
operators are both right shift operators, but they differ in how they handle the sign bit of the number being shifted. Here is a detailed explanation of each:
>>
Operator (Signed Right Shift)The >>
operator is known as the signed right shift operator. It shifts the bits of the number to the right by the specified number of positions. The key characteristic of the signed right shift is that it preserves the sign of the original number. This means that for positive numbers, it fills the vacated bit positions with zeros, and for negative numbers, it fills the vacated bit positions with ones (sign extension).
For a positive number:
For a negative number:
>>>
Operator (Unsigned Right Shift)The >>>
operator is known as the unsigned right shift operator or zero-fill right shift. It shifts the bits of the number to the right by the specified number of positions, but it always fills the vacated bit positions with zeros, regardless of the sign of the original number. This means that the result is always a non-negative number.
For a positive number:
For a negative number:
Sign Preservation:
>>
preserves the sign of the original number (sign extension).>>>
does not preserve the sign and always fills the vacated positions with zeros (zero-fill).Resulting Value:
>>
can result in negative numbers if the original number was negative.>>>
always results in a non-negative number...middle
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào