Reputation: 2072
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();
}
Upvotes: 0
Views: 108
Reputation: 43758
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
Upvotes: 1