On Tue, 28 Nov 2023 01:08:43 GMT, Brian Burkhalter <b...@openjdk.org> wrote:

>> It's equivalent. If you watch the implementations of other methods in the 
>> class where a mask out is made, this method is often used.
>
> Indeed it looks as if that is correct.

As specified in 
https://docs.oracle.com/javase/specs/jls/se21/html/jls-15.html#jls-15.19:
"If the promoted type of the left-hand operand is int, then only the five 
lowest-order bits of the right-hand operand are used as the shift distance. It 
is as if the right-hand operand were subjected to a bitwise logical AND 
operator & 
([ยง15.22.1](https://docs.oracle.com/javase/specs/jls/se21/html/jls-15.html#jls-15.22.1))
 with the mask value 0x1f (0b11111). The shift distance actually used is 
therefore always in the range 0 to 31, inclusive.
The value of n >>> s is n right-shifted s bit positions with zero-extension, 
where If n is negative and the type of the left-hand operand is int, then the 
result is equal to that of the expression (n >> s) + (2 << ~s). The added term 
(2 << ~s) cancels out the propagated sign bit.
Note that, because of the implicit masking of the right-hand operand of a shift 
operator, ~s as a shift distance is equivalent to 31-s when shifting an int 
value and to 63-s when shifting a long value."
So, since ~s == -s - 1, we have that -s  as a shift distance is equivalent to 
32-s.
Since (excessBits == 32 * numInts - numBits), then (excessBits == (32 - 
(numBits & 0x1f)) & 0x1f), because (x & 0x1f == x mod 32). Since the five 
lowest-order bits of 32 are 0, then the five lowest-order bits of (32 - 
(numBits & 0x1f)) are the same of (-numBits), and so (excessBits == (-numBits) 
& 0x1f), therefore (-1 >>> excessBits == -1 >>> -numBits).

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/16817#discussion_r1407635417

Reply via email to