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位)可以砍啊,这怎么算啊  
 

解决方案 »

  1.   

    You may refer to this article.
    http://en.wikipedia.org/wiki/Two's_complement
      

  2.   

    156 (10) = 00000000 00000000  00000000 10011100 (2)When this int number is converted to a byte number, only the last byte is stored, i.e. the last 8 bits, 10011100 (2).Now, let's study this number:
    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).
      

  3.   

    If you don't understand part (b), please study that article of two's complement representation.