473,386 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

boost::dynamic_bitset...

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

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!
Jul 22 '05 #1
2 6759
On Tue, 28 Sep 2004 07:28:07 +0200, "Sam Smith" <sa*@smith.com> 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[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!
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 notobvious 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
Jul 22 '05 #2
On Tue, 28 Sep 2004 13:55:54 +0100, Tom Widmer
<to********@hotmail.com> 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
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: rob | last post by:
hey every1, I've got alot of data to write out to file and it's all just 1's and 0's. It's all stored in 2 dimensional arrays of width 32 and varying height. At the moment it's all just...
2
by: ctick | last post by:
I try to make a member of "dynamic_bitset" of a dynamic size that is determined when its containing class is instantiated. The code has error when compiling: "N is undefined" something like that....
1
by: ctick | last post by:
Q1. boost::dynamic_bitset<>& get_bset() { boost::dynamic_bitset<> x(8); return x; } void main() { boost::dynamic_bitset<> y(8);
19
by: daniel | last post by:
1) is C++ smart enough to automatically use "bits" for bool or will a bool have the size of a charcter (byte). 2) The index of a vector is it an integer (4 byte) or a "long long" with 8 bytes or...
16
by: Jeff Flinn | last post by:
At the risk of raising the OT ire of some here, I'd like to know what might be done to raise the awareness of the boost libraries. I've found boost and it's libraries invaluable in my work for ~5...
5
by: Vince | last post by:
Hi, I would like to extract or insert some values (of different size) inside a buffer. I would like to have something like this : BYTE Buffer;
4
by: aaragon | last post by:
Hi everyone, I am trying to create a class but I would like to be able to chose between several data structures for one of its members. The basic idea is template < class Sequence =...
11
by: aaragon | last post by:
Hi everyone. I'm trying to write a class with policy based design (Alexandrescu's Modern C++ Design). I'm not a programmer but an engineer so this is kind of hard for me. Through the use of...
7
by: BubbaT | last post by:
What is the best way to pick up Boost? Thanks BubbaT
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.