Difference between >>> and >>

前端 未结 7 1089
情歌与酒
情歌与酒 2020-11-22 07:13

What is the difference between >>> and >> operators in Java?

7条回答
  •  有刺的猬
    2020-11-22 07:56

    >> 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 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.

提交回复
热议问题