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

Andrew Dinn adinn at redhat.com
Thu Apr 8 09:15:29 UTC 2021

On 08/04/2021 10:11, Daniel Fuchs wrote:
> On Thu, 8 Apr 2021 08:54:35 GMT, Sebastian Stenzel <github.com+1204330+overheadhunter 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.
> I agree with Sebastian. I believe the original code was clearer.
The same applies for the (value >>> 0) expressions that this patch also 
removes. The reason for keeping them is to emphasize the continuity with 
the (value >>> 24), (value >>> 16), etc cases that precede them.

Since none of this will make any difference to performance I think it is 
better to keep this code as is.


Andrew Dinn
Red Hat Distinguished Engineer
Red Hat UK Ltd
Registered in England and Wales under Company Registration No. 03798903
Directors: Michael Cunningham, Michael ("Mike") O'Neill

More information about the core-libs-dev mailing list