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

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Сергей Цыпанов-2
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.

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

Commit messages:
 - Merge branch 'master' into bitwise
 - Remove redundant '& 0xFF' from int-to-byte cast

Changes: https://git.openjdk.java.net/jdk/pull/2826/files
 Webrev: https://webrevs.openjdk.java.net/?repo=jdk&pr=2826&range=00
  Issue: https://bugs.openjdk.java.net/browse/JDK-8264896
  Stats: 50 lines in 14 files changed: 0 ins; 0 del; 50 mod
  Patch: https://git.openjdk.java.net/jdk/pull/2826.diff
  Fetch: git fetch https://git.openjdk.java.net/jdk pull/2826/head:pull/2826

PR: https://git.openjdk.java.net/jdk/pull/2826
Reply | Threaded
Open this post in threaded view
|

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

Sebastian Stenzel-2
On Thu, 4 Mar 2021 12:16:29 GMT, Сергей Цыпанов <[hidden email]> 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
Reply | Threaded
Open this post in threaded view
|

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

Daniel Fuchs-2
On Thu, 8 Apr 2021 08:54:35 GMT, Sebastian Stenzel <[hidden email]> 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.

I agree with Sebastian. I believe the original code was clearer.

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

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