RFR: 8264896: Remove redundant '& 0xFF' from int-to-byte cast

Sebastian Stenzel github.com+1204330+overheadhunter at openjdk.java.net
Thu Apr 8 08:57:17 UTC 2021


On Thu, 4 Mar 2021 12:16:29 GMT, Сергей Цыпанов <github.com+10835776+stsypanov at openjdk.org> wrote:

> When we do
> byte b1 = (byte) (value & 0xFF);
> we keep from int only 1 lower byte and exactly the same can be achieved with plain cast. See the test below:
> public class Main {
>   public static void main(String[] args) throws Exception {
>     IntStream.range(Integer.MIN_VALUE, Integer.MAX_VALUE).forEach(value -> {
>       byte b1 = (byte) (value & 0xFF);
>       byte b2 = (byte) value;
>       if (b1 != b2) {
>         throw new RuntimeException("" + value);
>       }
>     });
> }
> 
> Also tier1 and tier2 are both OK.

I don't think these masks have been added because they are _required_ but rather because they explicitly show the intention: You can immediately see that losing the MSBs of an int during the cast is _not_ just an error.

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

PR: https://git.openjdk.java.net/jdk/pull/2826


More information about the nio-dev mailing list