java int转byte型
package helloWorld;
public class helloWord {
public static void main(String[] args) {
byte b1 = (byte)300;
byte b2 = (byte)130;
System.out.println(b1);
System.out.println(b2);
}
}
输出结果为
44
-126
b1=300
300的二进制是
00000000 00000000 00000001 00101100
int转byte,截短,即b1=00101100,00101100对应的原码为44,所以输出44
b2=130
130的二进制是
00000000 00000000 0000000 10000010 130
int转byte,截短,即b2=10000010
10000010是补码,对应的原码为:
补码:10000010
反码:10000001 (补码-1)
原码:11111110 (除符号位,其他位取反)
11111110
-01111110
-126
所以输出-126
关于 原码、 反码、补码 可参考 https://blog.csdn.net/e3399/article/details/89290520