creating custom bit streams

Ok, I'm trying to create my own class I call bit_stream that handles i/o operations but through internal implementation can do i/o at the bit level. I'm essentially trying to find the best way to store the bits of an integer into a vector<bool> but I only need the bits that are significant, anything past the most significant bit is not needed.

The problem I keep running into is how to encode/write a character (or an int for that matter) to a binary file using only 7 bits rather than 8 bits. I've been directed by colleagues to use a bitset or vector<bool> for some of this but I still can't figure out the syntax or how exactly this works:

1
2
3
4
5
6
7
void encode_character(char c, vector<bool> & buffer)
{
	bitset<CHAR_WIDTH> bit_array(c);
	for(int i = (CHAR_WIDTH-1); i >= 0; i--) {
		buffer.push_back(bit_array[i]);
	}
}


This is how I initially encode the character, is there a better way of doing this or no?
btw, CHAR_WIDTH = 8
Last edited on
Topic archived. No new replies allowed.