[英]Java BigInteger testBit 64-bit long
这可能是一个愚蠢的问题,但我似乎无法找到一个简单的答案。
我正在读取一个64位(8字节) long
值,然后我尝试使用BigInteger.testBit
来查看是否设置了第63位,因为它被用作标志。
long value = 0x4000863; //This value is actually read from a file
Long.toBinaryString(value) = 100000000000000100001100011
BigInteger test = new BigInteger(Long.toString(value));
if (test.testBit(63)) {
//yay
}
else {
//boo
}
上面的代码就是我目前正在尝试的,它表示第63位没有设置。 因为它被存储了很长时间,我不认为我必须填补价值,或者我只是完全做错了什么?
任何意见或建议将不胜感激。
谢谢。
你在计算错误的位数:
public void test() {
// Binary - 100000000000000100001100011
// ^ This is bit 26
long value = 0x4000863;
// Binary - 1000000000000000000000000000000000000000000000000000000000000000
// ^ THIS is bit 63
long bigger = 0x8000000000000000L;
BigInteger test = new BigInteger(Long.toString(value));
System.out.println("L:" + Long.toBinaryString(value) + "\r\nB:" + test.toString(2) + "\r\nB63:" + test.testBit(63));
test = new BigInteger(Long.toString(bigger));
System.out.println("L:" + Long.toBinaryString(bigger) + "\r\nB:" + test.toString(2) + "\r\nB63:" + test.testBit(63));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.