简体   繁体   English

Java BigInteger testBit 64位长

[英]Java BigInteger testBit 64-bit long

This is probably a silly question, but I can't seem to find a simple answer. 这可能是一个愚蠢的问题,但我似乎无法找到一个简单的答案。

I'm reading in a 64-bit (8 byte) long value and I'm then trying to use the BigInteger.testBit to see if bit 63 is set, as it's being used as a flag. 我正在读取一个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
}

The above code is what I'm currently trying, and it says bit 63 is not set. 上面的代码就是我目前正在尝试的,它表示第63位没有设置。 As it's being stored as a long, I didn't think I'd have to pad the value, or am I just doing something wrong entirely? 因为它被存储了很长时间,我不认为我必须填补价值,或者我只是完全做错了什么?

Any input or advice would be greatly appreciated. 任何意见或建议将不胜感激。

Thanks. 谢谢。

You are counting the bits wrong: 你在计算错误的位数:

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.

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