0

The BYTEMASK flag in the sample code keeps the value from being signed-extended when it's converted to an int. How is this flag preventing the conversion form being sign-extended?

private final static int BYTEMASK = 0xFF;

private static String byteArrayToDecimalString(byte[] bArray) {
        StringBuilder rtn = new StringBuilder();
        for(byte b : bArray) 
            rtn.append(b & BYTEMASK).append(" ");
        return rtn.toString();
    }
dcrearer
  • 1,972
  • 4
  • 24
  • 48

1 Answers1

1

As others have already mentioned in comments, the bytemask does not prevent the sign extension, instead it undoes it. For example:

0x80 -- sign extend -> 0xffffff80 -- mask with 0xff -> 0x00000080
Henry
  • 42,982
  • 7
  • 68
  • 84