import java.lang.System; public class test
{
public static void main(String args[])
{
byte a =67;
byte b =89; byte c = (byte)(a + b);
System.out.println(c);
}
} 教材上说是byte char short运算时都先化成 int型计算 这里的计算结果是-100 因为BYTE最高位是-128 --- 127
这里的结果是-100
这里运算是INT型转换成BYTE行,BYTE型是1字节(8位)
所以看掉3字节就可以了
但是我把a+b =156 转换成2进制是 10011100 哪里的3字节(24位)可以砍啊,这怎么算啊
{
public static void main(String args[])
{
byte a =67;
byte b =89; byte c = (byte)(a + b);
System.out.println(c);
}
} 教材上说是byte char short运算时都先化成 int型计算 这里的计算结果是-100 因为BYTE最高位是-128 --- 127
这里的结果是-100
这里运算是INT型转换成BYTE行,BYTE型是1字节(8位)
所以看掉3字节就可以了
但是我把a+b =156 转换成2进制是 10011100 哪里的3字节(24位)可以砍啊,这怎么算啊
http://en.wikipedia.org/wiki/Two's_complement
10011100 (2)a) the first left digit 1 indicates that this number is a negative number.
b) To get the decimal value, invert the bit and add 1 to it:
0011100
-->1100011
-->11001001100100 (2) = 100 (10)Now, we know that number 10011100 (2) = -100 (10).