问题描述
Java中>>>
和>>
运算符有什么区别?
What is the difference between >>>
and >>
operators in Java?
推荐答案
>>
是算术右移,>>>>
是逻辑右移.
>>
is arithmetic shift right, >>>
is logical shift right.
在算术移位中,符号位被扩展以保持数字的符号性.
In an arithmetic shift, the sign bit is extended to preserve the signedness of the number.
例如:以 8 位表示的 -2 将是 11111110
(因为最高有效位具有负权重).使用算术移位将其右移一位将为您提供 11111111
或 -1.然而,逻辑右移并不关心该值是否可能代表一个有符号数;它只是将所有内容向右移动并从左侧填充 0.使用逻辑移位将我们的 -2 右移一位将得到 01111111
.
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 you 11111111
, 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 give 01111111
.
这篇关于>>> 之间的区别和 >>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!