我正在查看 SE6 中 java.io
的 DataInputStream.readLong()
的实现:
private byte readBuffer[] = new byte[8];
public final long readLong() throws IOException {
readFully(readBuffer, 0, 8);
return (((long)readBuffer[0] << 56) +
((long)(readBuffer[1] & 255) << 48) +
((long)(readBuffer[2] & 255) << 40) +
((long)(readBuffer[3] & 255) << 32) +
((long)(readBuffer[4] & 255) << 24) +
((readBuffer[5] & 255) << 16) +
((readBuffer[6] & 255) << 8) +
((readBuffer[7] & 255) << 0));
鉴于readBuffer[]是一个字节数组,为什么需要&
每个字节为255?
当单个字节转换为 long
时,long 的剩余位 (9-64) 不应该自动设置为零,从而呈现 &
> 不必要吗?
最佳答案
java 的 byte 类型是有符号的,所以 0xff (255) == -1,在从 byte 扩展到 int/long 的过程中 - 有符号的值被保留,所以如果你只有代码:
final byte a = (byte)0xff;
final long b = a;
System.out.println(b); // output here is -1, not 255
所以,这里有一个技巧:
final byte a = (byte)0xff;
final long b = a & 0xff; // binary and between byte A and int 0xff
System.out.println(b); // output here is 255
因此,由于符号扩展,第一个字节变量 a 被提升为 int (并变为 0xffffffff),然后我们通过按位 AND 来截断它
关于java - 将字节转换为 long : Why do some implementations bitwise-AND each byte with 0xff?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29908516/