dynamic_bitset taking a buffer...

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!

Boost Rookie <boostrookie <at> yahoo.com> writes:
I should specify something called Block/BlockInputIterator as a template parameter? I do not know what that is.
Playing around a little more made me realize that the following is ok: unsigned char data[5] = {211,14,97,42,31}; boost::dynamic_bitset<unsigned char> bitbuf(data,&data[5]); So, unsigned char specifies a Block and the unsigned char* is the BlockInputIterator - alright. It wasn't that obvious for me as a newbie...
After construction I do not see how to extract the bits!? Is it only possible to extract one bit at a time?!?!?
I have not find a way around this yet :-( To make myself clear, I'd like to extract a range of bits, preferably by specifing the start bit position and the number of bits to extract. I can see that there is a to_block_range but it just extracts the complete bitset. Now, another issue has surfaced. When iterating the bitset I see that the bits are stored in a "weird" format (at least it is weird to me)?! for (int i=0; i<5*8; ++i) { bool bit_i = bitbuf[i]; std::cout << bit_i; } Instead of the expected sequence: 1101001100001110011000010010101000011111 the loop prints: 1100101101110000100001100101010011111000 I.e. each byte has its bits in the reversed order! As a newbie it is not obvious to me why this is so... And the confusion is total after this: std::cout << bitbuf; which decides to print the sequence: 0001111100101010011000010000111011010011 Please help me figure the rationale behind this out!
participants (1)
-
Boost Rookie