最后一段代码究竟在做什么?

时间:2013-03-04 01:42:01

标签: java

protected boolean[] bitArray = new boolean[8];

protected void readNextByte() throws IOException {

    latestReadByte = reader.read();
    int decimtalTal = latestReadByte

    for(int n= 0; n < 8; n++){
        int pos = (int)Math.pow(2, n);

        bitArray[7-n] = (decimalTal & pos) == pos;  // THIS LINE

        // what is the bitwise And in bracket == pos supposed to mean?
    }
}

2 个答案:

答案 0 :(得分:2)

bitArray[7-n] =赋值右侧的代码正在测试是否设置了decimalTal的位n。如果该位置1(非零),则计算结果为true;如果该位清零(零),则计算结果为假。

答案 1 :(得分:0)

@VGR是正确的,但是当你在将来遇到类似的代码时指出一个小的微妙之处:

(decimalTal & pos) == pos测试 all pos中的位是否也设置为decimalTal

(decimalTal & pos) != 0测试是否 pos中的任何位也设置为decimalTal

在这个例子中,pos只有1位设置,因为它是2的幂,所以它并不重要。

相关问题