From: Boost Rookie (boostrookie_at_[hidden])
Date: 2004-09-27 02:26:28


Hi,

I've just started playing around with Boost.
I have a specific need which I hope dynamic_bitset can fulfill.

I'd like to have a dynamically sized bitbuffer which is
initialized with a data stream of bits (this should be
quite efficient). Then I'd like to extract the bits
from that bitbuffer as specified in a protocol, e.g.
bit 0-3 : version
bit 4-5 : whatever
bit 6 : ack
bit 7-9 : whatever
and so on...

So something like this is what I think should be possible to do:

// Note: Original code simplified to examplify...

// Read DATALEN bytes from socket
char buffer[DATALEN];
int read_bytes = read(socket,buffer,DATALEN);

// Init a bit buffer (efficiently)
boost::dynamic_bitset<unsigned char> bitbuf(buffer,read_bytes);

// Extract different data, as specified by the communication protocol
int version = bitbuf.get(0,4); // Extract 4 bits starting at position 0
int whatever = bitbuf.get(4,2); // Extract 2 bits starting at position 4
bool ack = bitbuf.get(6,1) // Extract 1 bit at the 6th position
and so on...

Reading docs at http://www.boost.org/libs/dynamic_bitset/dynamic_bitset.html
shows that my shot at using dynamic_bitset is totally wrong! I cannot construct
the bitbuf in that way. Instead, I should specify something called
Block/BlockInputIterator as a template parameter? I do not know what that is.

Looking at the examples at that page doesn't help either. Only different
intergers are stored in the dynamic_bitset...

It seems that a string of 1's and 0's can be stored but I really do not
want to convert my original data buffer into a std::string and then
convert that into a bitbuffer.

After construction I do not see how to extract the bits!? Is it only possible
to extract one bit at a time?!?!?

Is dynamic_bitset designed to handle problems like this or am I trying to do
something that is not possible? If dynamic_bitset can't help, is there
something else in boost or elsewhere that is designed to solve problems like
this?

Thanks in advance!