我有以下方法:
int convert3ByteChunkTo24Bits(final byte[] bytes) {
int bitsFor3ByteChunk = 0;
bitsFor3ByteChunk = bitsFor3ByteChunk | (bytes[0] & 0b11111111);
bitsFor3ByteChunk = bitsFor3ByteChunk << 8;
bitsFor3ByteChunk = bitsFor3ByteChunk | (bytes[1] & 0b11111111);
bitsFor3ByteChunk = bitsFor3ByteChunk << 8;
bitsFor3ByteChunk = bitsFor3ByteChunk | (bytes[2] & 0b11111111);
return bitsFor3ByteChunk & 0b00000000_11111111_11111111_11111111;
}
该方法的目的是追加传递的字节并以 int 格式返回。这是我的一个测试方法:
void testConvert3ByteChunkTo24Bits() {
final byte ca = (byte) 0xCA;
final byte fe = (byte) 0xFE;
final byte ba = (byte) 0xBA;
final byte[] man = {ca, fe, ba};
final int bytesIn24Bits = base64EncoderHelper.convert3ByteChunkTo24Bits(man);
System.out.println(bytesIn24Bits);
}
结果的输出将是:
13303482
二进制文件是:
00000000110010101111111010111010
这是正确的,并且是预期的结果,因为:
0xCA = 11001010
0xFE = 11111110
0xBA = 10111010
所以,到目前为止一切顺利。只有一件事我不明白,为什么我需要逻辑与,如
(bytes[0] & 0b11111111)
当我不进行按位与运算时,所以如果实现如下:
int convert3ByteChunkTo24Bits(final byte[] bytes) {
int bitsFor3ByteChunk = 0;
bitsFor3ByteChunk = bitsFor3ByteChunk | (bytes[0]);
bitsFor3ByteChunk = bitsFor3ByteChunk << 8;
bitsFor3ByteChunk = bitsFor3ByteChunk | (bytes[1]);
bitsFor3ByteChunk = bitsFor3ByteChunk << 8;
bitsFor3ByteChunk = bitsFor3ByteChunk | (bytes[2]);
return bitsFor3ByteChunk & 0b00000000_11111111_11111111_11111111;
}
结果将是:
00000000111111111111111110111010
我错过了什么?是什么意思
bytes[0] & 0b11111111
修复这里吗? (我通过尝试发现了这种错误,并且仍然好奇它为什么有效......)
最佳答案
在:
bitsFor3ByteChunk = bitsFor3ByteChunk | (bytes[0] & 0b11111111);
bytes[0]
首先扩展为 int
值(整数均以补码表示)。那么对于像 0xFF
这样存储在 byte
中的值,相应的 int
值将是 0xFFFFFFFF
。然后对其进行屏蔽以保留低 8 位。
关于java - 将字节与 0b1111_1111 按位与有何作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43102292/