Set a C++ bitset from a binary input steam
I ha开发者_如何学Gove an input stream from a binary file. I want to create a bitset for the first 5 bits of the stream. Here is the code I have so far:
ifstream is;
is.open ("bin_file.out", ios::binary );
bitset<5> first_five_bits;
is >> first_five_bits; // always is set to default 00000
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>
Streams don't work with bits, so you should read in a byte and set that to the bitset.
Not compiled, not tested:
char c;
is >> c;
bitset<5> first_five_bits(c >> 3);
精彩评论