boost::dynamic_bitset...

Discussion in 'C++' started by Sam Smith, Sep 28, 2004.

  1. Sam Smith

    Sam Smith Guest

    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!
    Sam Smith, Sep 28, 2004
    #1
    1. Advertising

  2. Sam Smith

    Tom Widmer Guest

    On Tue, 28 Sep 2004 07:28:07 +0200, "Sam Smith" <> wrote:

    >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
    Tom Widmer, Sep 28, 2004
    #2
    1. Advertising

  3. Sam Smith

    Tom Widmer Guest

    On Tue, 28 Sep 2004 13:55:54 +0100, Tom Widmer
    <> wrote:

    >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
    Tom Widmer, Sep 28, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard Latter
    Replies:
    2
    Views:
    2,897
    Julie
    May 17, 2004
  2. Steve Knight
    Replies:
    2
    Views:
    754
    Steve Knight
    Oct 10, 2003
  3. Toby Bradshaw
    Replies:
    6
    Views:
    1,745
    Kai-Uwe Bux
    Jun 2, 2006
  4. Colin Caughie
    Replies:
    1
    Views:
    713
    Shooting
    Aug 29, 2006
  5. Misiu
    Replies:
    3
    Views:
    2,370
    Misiu
    Jan 31, 2007
Loading...

Share This Page