我是这样的低级操作的新手,我希望有人能指出我在这里犯的明显错误。
//Input value - 00111100
//I want to get the value of the bits at indexes 1-3 i.e 0111.
byte mask = (byte)0x00001111; // This gives 17 not the 15 I'd expect
byte shifted = (byte)(headerByte >> 3);
//shifted is 7 as expected
byte frameSizeValue = (byte)(shifted & mask); //Gives 1 not 7
看起来问题在于掩码的定义方式,但我不知道如何解决它。
首先0x00001111
是十六进制的,它比255
- 16^3 + 16^2 + 16 + 1 = 4369
and byte
溢出。看here https://stackoverflow.com/questions/4013441/why-are-there-no-binary-literals-in-java如何表示二进制数或仅使用shifted & 15
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)