Remember:
Java byte signed. So:
byte test = (byte)255; System.out.println(test);
will output: -1
and
byte test = (byte)255; System.out.println(test == 255);
will print false but
byte test = (byte)255; System.out.println((test & 255) == 255);
will do what I think you want to achieve (in this case prints true ).
To get unsigned (byte) values, use (array[index] & 255) .
You will need to do the masking with 0xff everywhere, otherwise you will get the sign of extended integers for byte values ββgreater than 127.
For comparison, you can also cast to byte (for example, if(test == (byte)255) ), but I think that you need to stick to a single conversion, so I recommend switching to masking & 0xff .
source share