The difference between >> and >>>


The difference between >> and >>>

1. >>: right shift operator, num >> 1, which is equivalent to dividing num by 2.

Move all the numbers to the right by the corresponding number of digits in binary form, remove the low bits and discard them, and fill the high bit vacant bits with the sign bit, that is, zeros are filled for positive numbers, 1 is filled for negative numbers, and the sign bit remains unchanged.

  • -1 is expressed in 32-bit binary as: 11111111 11111111 11111111 11111111, -1>>1: bitwise right shift, sign bit unchanged, still get 11111111 11111111 11111111 11111111, so the value is still -1

2. >>>: binary right shift zero padding operator.

The value of the left operand is right-shifted by the number of digits specified by the right operand, and the shifted vacancy is filled with zeros. For example, in value >>> num, num specifies the number of digits to be shifted.

  • -1 is expressed in 32-bit binary as: 11111111 11111111 11111111 11111111, -1 >>> 1: bitwise right shift, the empty space is filled with 0, and 01111111 11111111 11111111 11111111 is obtained, so the value is 2147483647

Guess you like

Origin blog.csdn.net/weixin_54040016/article/details/128062307