boost::dynamic_bitset...

S

Sam Smith

I've just started to play around with boost in general and the
dynamic_bitset in particular. I want to use the dynamic_bitset to hold an
array of data as stream of bits:

unsigned char data[5] = {211,14,97,42,31};
boost::dynamic_bitset<unsigned char> bitbuf(data,&data[5]);


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;
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!

Another issue I have is how to extract the bits!?
Is it only possible to extract one bit (using operator[]) or all bits (using
to_block_range)?!?!? I'd much rather extract a range of bits starting at a
specified bit position and a given number of bits:

unsigned short value = bitbuf.get(3,12); // Extract 12 bits starting at
pos 3

Thanks in advance!
 
T

Tom Widmer

I've just started to play around with boost in general and the
dynamic_bitset in particular. I want to use the dynamic_bitset to hold an
array of data as stream of bits:

unsigned char data[5] = {211,14,97,42,31};
boost::dynamic_bitset<unsigned char> bitbuf(data,&data[5]);


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;
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!


Well, that depends on whether you think of the least significant bit
as being bit 0 or the most significant. You expect the MSB, the
authors went for the LSB. MSB first makes sense given that when
writing down binary numbers we typically write the MSB first, but LSB
makes sense from a programming point of view:
bool get(size_t i)
{
size_t block_num = i / BLOCK_BIT_COUNT;
size_t bit_pos = i % BLOCK_BIT_COUNT;
return (blockarray[block_num] >> bit_pos) & 1;
}

To do it the other way around involves doing:
size_t bit_pos = BLOCK_BIT_COUNT - 1 - (i % BLOCK_BIT_COUNT);
which is clearly slightly slower.

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!

Well, I suppose bit[0] is the LSB, so it should go at the end, to
conform to the usual way of writing binary I mentioned above. I agree
this might be confusing.
Another issue I have is how to extract the bits!?
Is it only possible to extract one bit (using operator[]) or all bits (using
to_block_range)?!?!? I'd much rather extract a range of bits starting at a
specified bit position and a given number of bits:

unsigned short value = bitbuf.get(3,12); // Extract 12 bits starting at
pos 3

I think you have to do this yourself. Something like this (uncompiled,
untested) code:

template <class Block, class Alloc, class BlockOutputIterator>
BlockOutputIterator
get_bit_range(
boost::dynamic_bitset<Block, Alloc> const& b,
std::size_t begin,
std::size_t end,
BlockOutputIterator out)
{
Block value = 0;
for(int pos = 0; begin != end; ++begin)
{
value |= (b.test(begin) << pos++);
if (pos == boost::dynamic_bitset<Block,
Alloc>::bits_per_block)
{
pos = 0;
*out = value;
++out;
value = 0;
}
}
return out;
}

//...
unsigned char value[2];
get_bit_range(bitbuf, 3, 12, value);

Tom
 
T

Tom Widmer

Well, that depends on whether you think of the least significant bit
as being bit 0 or the most significant.

That should have read:

Well, that depends on whether you think of bit 0 being the least
significant bit or the most significant.

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top