1

I am trying to understand this bit of code, the method addBittoTree needs a boolean to be passed through. I am not quite sure as to what it is checking. I do not understand why there is an ampersand for currentByte and -128, is it using that as an addition operator?

byte currentByte = dis.readByte();
tree.addBitToTree( (currentByte & -128) == -128 );
4

1 回答 1

5

-128 的补码是

1000 0000

假设您的 currentByte 设置了第一位:

    1000 0000 // -128
        &     // bitwise logical and
    1010 1010 // currentByte (example)
is
    1000 0000 // -128

与 ( ==) 进行比较-128,因此您正在传递boolean参数true

另一个未设置第一位的示例:

    1000 0000 // -128
        &     // bitwise logical and
    0011 1110 // currentByte (example)
is
    0000 0000 // 0

与 ( ==) 进行比较-128,因此您正在传递boolean参数false

由于这种做法总是传递true给方法,当第一个位被设置false时,当它没有被设置时,我们知道所有正数都没有第一个位被设置,而所有负数都有,它相当于简单地写:

tree.addBitToTree(currentByte < 0);
于 2013-02-27T11:41:07.513 回答