How a byte
value b
when uses as int
in the Integer.toBinaryString() have a binary value much more than byte
type can contain? I thought s1
should be in range [0, 11111111]. And how this bitwise operator (b & 0B11111111
) changes the situation? It seems that it changes nothing because 0 & 1 = 0 and 1 & 1 = 1
public class Test
{
public static void main(String[] args)
{
byte b = (byte) -115;
String s1 = Integer.toBinaryString(b);
String s2 = Integer.toBinaryString(b & 0B11111111);
System.out.println(s1); // 11111111111111111111111110001101
System.out.println(s2); // 10001101
}
}