简体   繁体   English

用Java解码一个字节

[英]decoding a byte in java

The code that I can't understand does this : 我不明白的代码做到了:

int decodeTimeStampByte(final byte timeByte) {
   return timeByte & (~64);
}

So for instance, if I get the byte 4c (which is ASCII L), what exactly would the above function do to it? 因此,例如,如果我得到字节4c(即ASCII L),那么上面的函数将对它执行什么操作? How about the byte 44? 字节44怎么样?

The '~' is bitwise 'not', so 64 = 0x40 = 0100000b and ~64 = 1011111b (the lower 5 bits set). '〜'是按位的'not',因此64 = 0x40 = 0100000b和〜64 = 1011111b(设置的低5位)。

Then '&' is bitwise 'and' and it leaves just the 5 lower bits of timeByte. 然后,“&”是按位“与”,它只剩下timeByte的低5位。 So, basically, it is a truncation of timeByte to 0..63 range. 因此,基本上,它是timeByte到0..63范围的截断。

decodeTimeStampByte(4c) = 0xC (12) 解码时间标记字节(4c)= 0xC(12)

decodeTimeStampByte(44) = 44 解码时间StampByte(44)= 44

PS Yes, I forgot the higher bits. PS:是的,我忘记了更高的位。 ~64 = 1011111b. 〜64 = 1011111b。

It is either a bug in the code or some intention to leave the sign bit (the 7-th bit) in place. 这可能是代码中的错误,也可能是有意将符号位(第7位)保留在原位。

PPS Seems like an ancient bit-hack to squeeze some more performance PPS似乎就像一个古老的黑客技术,可以榨取更多性能

该代码将清除第6位。但是,如果设置了第7位,它将把所有位设置为8到31(由于将字节转换为int)

This function is returning the lower 6 bits for positive values and clearing the 7th bit for negative values. 此函数返回低6位为正值,并清除第7位为负值。 So, 2^6=64, 64 = 1000000 in binary, ~64 = 0111111 in binary would mask values between [0..63] and [-128..-65] of timeByte. 因此,二进制值2 ^ 6 = 64,二进制数64 = 1000000,二进制数〜64 = 0111111将掩盖timeByte的[0..63]和[-128 ..- 65]之间的值。

This function is returning the lower 6 bits for positive values and clearing the 7th bit for negative values. 此函数返回低6位为正值,并清除第7位为负值。 So, 2^6=64, 64 = 1000000 in binary, ~64 = 0111111 in binary would mask values between [0..63] and [-128..-65] of timeByte . 所以, 2^6=64, 64 = 1000000二进制, ~64 = 0111111二进制将掩盖之间的值[0..63][-128..-65]timeByte

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM