获取数组中每个 int 的 LSB 并将它们组合起来创建一个字节

Take the LSB of each int in an array and combine them to create a byte

所以现在我有这个

unsigned char getlsbs(unsigned char *p){
    int r;
    unsigned char newByte, temp;
    newByte = 0;

    for(r = 0; r < 8; r++){
            temp = p[r];
            temp &= -temp; // Gets the LSB of p[r]
            ((newByte & (1 << r)) >> r) |= temp; //This line does not work
    }
    return newByte;
}

对于不起作用的行,我正在尝试将 newByte 的第 rth int 设置到 LSD 中,这样我就可以将 00000000 变成类似 10100001 的任何帮助,我们将不胜感激

不能给表达式赋值。为了简化问题:a + 1 = b 不起作用。将其重写为 a = b - 1

我会这样做:

for(r = 0; r < 8; r++){
    newByte |= (p[r] & 1) >> r;
}

将位累加到目标中的一种常见方法是将先前的值左移就地,然后在新位中进行或运算:newByte = (newByte << 1) | (temp & 0x01) 在上面的代码中,行的左侧也就是说,"This line does not work" 似乎不是赋值操作的有效目标。