我有一个writeBit方法,应将int i的最低有效位写入char类型的缓冲区buff中,然后递增位缓冲区索引。
我不确定我所提供的信息是否正确,任何输入都将受到赞赏。
private:
char buff; // buffer
int num_bits; // num of bits written to buff
std::ostream& os_ref;
public:
// Skipping the constructor and ostream& for brevity
int writeBit(int i) {
// flush buffer if full
if(num_bits == 8)
flush();
// write least significant bit into the buffer at the current index.
int lb = i & 1;
buff = buff & num_bits; // not sure about this line
buff = lb;
num_bits++;
// return current index
return num_bits; // do I return nbits as current index?
}
最佳答案
我想类似的东西应该起作用,让我知道:
buff |= (i & 1) << nbits;
确保在
buff
期间flush()
设置为0基本上,
i & 1
获取最后一个有效位,<< nbits
将该位左移nbit次,并将该位添加到buff中。关于c++ - 如何将最低有效位写入缓冲区?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18396490/