从二进制输入流设置C ++ bitset

时间:2010-06-03 14:46:25

标签: c++ bits

我有来自二进制文件的输入流。我想为流的前5位创建一个位集。这是我到目前为止的代码:

ifstream is;    
is.open ("bin_file.out", ios::binary );

bitset<5> first_five_bits;

is >> first_five_bits; // always is set to default 00000

2 个答案:

答案 0 :(得分:2)

char c;
if( ! cin.get(c) ) throw ROFL();  // return error, flip bit, call mom
bitset<5> first_five_bits(c >> (CHAR_BIT-5));  // CHAR_BIT in <climits>

答案 1 :(得分:1)

Streams不能使用位,所以你应该读取一个字节并将其设置为bitset。

未编译,未经测试:

char c;
is >> c;
bitset<5> first_five_bits(c >> 3);