RFR: 5038439: Warning message for literal shift amounts outside the canonical domain [v2]

Roger Riggs rriggs at openjdk.org
Mon Nov 3 19:15:13 UTC 2025


On Mon, 3 Nov 2025 18:31:54 GMT, Archie Cobbs <acobbs at openjdk.org> wrote:

>> One way to read `v << -n` is: "Shift out everything to the left, except for the `n` least significant bits".
>> Analogously for `v >>> -n`: "Shift out everything to the right, except for the `n` most significant bits".
>> 
>> To rotate `v` by `n` bits to the left, for example, one can write `v << n | v >>> -n` (regardless of the width of `v`)
>
> Hmm, obviously this is a judgement call and I'm curious what others think.
> 
> My opinion is that I still think there should be a warning for shifts of -1.
> 
> I mean, maybe to a _real_ hacker, then of course `foo << -1` makes perfect sense because it's just stating the "obvious" which is "shift the low order bit into the high order position and set all the other bits to zero", right?? :)
> 
> I just don't think the average Java programmer automatically understands that.
> 
> Yes, it's an idiom or "handy trick", but if hasn't attained the status of being universally recognized then it doesn't deserve a special exception.
> 
> On the other hand, excluding -1 from the warning would be less disruptive (at least, to the real hackers out there), and that has its own merits. So I don't have a super strong opinion about it.

This might be worth a corpus scan to see how common it is to shift by -1 or other constants, either int or long.
Someone, might interpret `v << -1` as a right shift, is it more likely to misinterpret it as using more than 5/6 bits of the shift or considering it signed.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/27102#discussion_r2487576716


More information about the compiler-dev mailing list