>>> - logical shift right operator in Java .
It shifts to zero on the left, and does not preserve the sign bit. The blog post author even provides an implementation in C ++:
mid = ((unsigned int)low + (unsigned int)high)) >> 1;
... if you correctly shift unsigned numbers, saving the sign bit does not make sense (since there is no sign bit), so the compiler obviously uses logical shifts, not arithmetic ones.
The code above uses MSB (32nd bit, assuming 32-bit integers): adding low and high , which are non-negative integers and correspondingly placed in 31 bits, never overflow the full 32 bits, but they apply to MSB By moving it to the right, the 32-bit number is effectively divided by two, and the 32nd bit is cleared again, so the result is positive.
The truth is that the >>> operator in Java is just a workaround for the language not providing unsigned data types.
Alexander Gessler
source share