Unsigned right shift (`>>>`) moves an integer’s bit representation rightward and fills the left side with zeros. For a positive `int`, shifting right one bit is equivalent to dividing by two. For a negative value, it instead treats the bit pattern as a positive value larger than `MAX_VALUE`. section 4.2.1
int a = 1_000_000_000; int b = 2_000_000_000; int average = (a + b) >>> 1; #1
Use `(a + b) >>> 1` to compute the midpoint of two nonnegative `int` values when their sum may overflow. The shift recovers the intended midpoint from the wrapped bit pattern. section 4.2.1
Use this overflow workaround carefully:
- Restrict it to inputs that cannot be negative; the trick does not produce a correct average for negative inputs. section 4.2.1
- Prefer computing the intermediate sum as `long` when possible; this is simple, only slightly slower, and also supports averages of more than two `int` values. section 4.2.1
- Watch large-input algorithms such as binary search, where overflow can stay hidden until an array exceeds 2^30 elements. section 4.2.1
