import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;public class ShiftOperator { public static void main(String[] args) throws IOException {
byte[] arr = { 0,0,0,-1};
ByteArrayInputStream byteInput = new ByteArrayInputStream(arr);
DataInputStream dataInput = new DataInputStream(byteInput);
System.out.println(dataInput.readInt()); dataInput.reset();
byte b1 = dataInput.readByte();
byte b2 = dataInput.readByte();
byte b3 = dataInput.readByte();
byte b4 = dataInput.readByte();
System.out.println(b1 + " " + b2 + " " + b3 + " " + b4);
System.out.println((b1 << 24) + (b2 << 16) + (b3 << 8) + b4);
}
}输出:
255
0 0 0 -1
-1最后一行的输出我觉得不应该是-1,而应该是255才对啊
import java.io.DataInputStream;
import java.io.IOException;public class ShiftOperator { public static void main(String[] args) throws IOException {
byte[] arr = { 0,0,0,-1};
ByteArrayInputStream byteInput = new ByteArrayInputStream(arr);
DataInputStream dataInput = new DataInputStream(byteInput);
System.out.println(dataInput.readInt()); dataInput.reset();
byte b1 = dataInput.readByte();
byte b2 = dataInput.readByte();
byte b3 = dataInput.readByte();
byte b4 = dataInput.readByte();
System.out.println(b1 + " " + b2 + " " + b3 + " " + b4);
System.out.println((b1 << 24) + (b2 << 16) + (b3 << 8) + b4);
}
}输出:
255
0 0 0 -1
-1最后一行的输出我觉得不应该是-1,而应该是255才对啊
b1 << 24 = 0
b2 << 16 = 0
b3 << 8 = 0
b4 = -1你说下面语句输出什么?
System.out.println((b1 << 24) + (b2 << 16) + (b3 << 8) + b4);
b1 << 24 应该是 00000000 00000000 00000000 00000000
b2 << 16 应该是 00000000 00000000 00000000
b3 << 6 应该是 00000000 00000000
b4 应该是 11111111加起来就是 00000000 00000000 00000000 11111111
System.out.println((b1 << 24) + (b2 << 16) + (b3 << 8) + b4);输出不应该是255吗?
System.out.println( b4 & 0x000000ff); // 填充到最低字节PS:因为 b1,b2,b3 为byte类型,所以它们怎么‘与’,'或',也是8位.得到的值应该是:
11111111 而不是:
00000000 00000000 00000000 11111111
byte[] arr = { 20,0,0,0};
应该输出为:
335544320
20 0 0 0
20
可是实际却输出为:
335544320
20 0 0 0
335544320这是为什么呢
会是int;例子1:
byte b = -4;
System.out.println(b>>>2);//输出为:1073741823分析:
>>>表示无论原值是正还是负,一律在高位处补0
b转化为二进制为: 11111111 11111111 11111111 11111011
经过b>>>2运算,结果为00111111 11111111 11111111 11111110,转化为十进制为1073741823例子2:
byte[] arr = { 0,-1,0,0};
ByteArrayInputStream byteInput = new ByteArrayInputStream(arr);
DataInputStream dataInput = new DataInputStream(byteInput);
System.out.println(dataInput.readInt());dataInput.reset();
byte b1 = dataInput.readByte();
byte b2 = dataInput.readByte();
byte b3 = dataInput.readByte();
byte b4 = dataInput.readByte();
System.out.println(b1 + " " + b2 + " " + b3 + " " + b4);
System.out.println((b1 << 24) | (b2 << 16) | (b3 << 8) | b4);输出:
16711680
0 -1 0 0
-65536分析:
数组arr在内存中连续的数值为:00000000 11111111 00000000 00000000,十进制为16711680,
所以第一次输出为16711680重点分析第三次输出:
b1向左位移后为: 00000000 00000000 00000000 00000000
b2向左位移前要先转化为int:11111111 11111111 11111111 11111111
b2向左位移后为: 11111111 11111111 00000000 00000000
b3向左位移后为: 00000000 00000000 00000000 00000000
b4向左位移后为: 00000000 00000000 00000000 00000000结果为:11111111 11111111 00000000 00000000
取反为:
10000000 00000000 11111111 11111111,十进制为65535,再加一,再加符号就为-65536
例子3:
int num1=129;
byte num2=(byte)num1;
System.out.println(num2);//打印出:-127int num1=128;
byte num2=(byte)num1;
System.out.println(num2);//打印出:-128int num1=256;
byte num2=(byte)num1;
System.out.println(num2);//打印出:0int num1=257;
byte num2=(byte)num1;
System.out.println(num2);//打印出:1首先要搞清楚,byte型是8位.print函数永远是调用该类型的toString方法,也就是说将byte型作为一个有符号整数进行输出.
第二点,机器数的存放是用补码,有了这两条,就可以来分析楼主说的题了
129:00000000 00000000 00000000 10000001(整数的补码就是其本身)
转换成byte:10000001
注意首位变成1,说明这是个负数!对100000001减1去反加负号(补码-原码),得-01111111,即-127 128: 00000000 00000000 00000000 10000000
byte:10000000
减一取反加负号:-10000000,即-128 256: 00000000 00000000 00000001 00000000
byte:00000000 即0 257: 00000000 00000000 00000001 00000001
byte:00000001 即1