3

I'm having trouble initializing my dynamic_bitset. I have this code:

for(int j=(data1.length()-1); j>=0; j--){  
    x[j] = data1[j];  
}

Where data1 is a 32-bit binary number being read in from a file.

When I output the values for data1[j], it outputs the correct values I need to assign to x, but when I output x (after the supposed initialization), x is all ones.

What am I doing wrong?

EDIT: I'm trying to decompress code. The compressed code is given in lines that are 32 bits long. I want to put everything into one structure for easy access.

My code to initialize x:

int size = numLines * 32; //numLines is the number of lines of 32 bits
boost::dynamic_bitset<> x(size);  

EDIT2:
There are 8 lines of 32-bits each. The for loop is nested within a while loop that accesses each of those 8 lines of 32-bit binary and assignes that value to data1, so data1 changes with every iteration of the while loop.
First iteration:

data1: 10001001110000001001011100110011  

Second iteration:

data1: 00110011001110001010001110000000  

Third iteration:

data1: 00000011100000000000000000000000 

etc....

I want x to be a long bitset containing all those values concatenated with each other.

EDIT3:
Trying one method:

for(int i=0; i<numLines; i++){
    d.append(data1);
}
boost::dynamic_bitset<> x(std::string(d));
Lady_ari
  • 433
  • 3
  • 8
  • 19

1 Answers1

1

See this boost example for how to populate a dynamic_bitset.

Firstly, I would change your loop to:

for (unsigned i = 0; i < data1.length(); i++) {
    x[i] = data1[i];
}

Response to Question Edit:

You can construct a dynamic_bitset using a string:

boost::dynamic_bitset<> x(std::string(data1));

So simply read the whole string in (provided it's not too big), throw away any whitespace so you simply have one long string of 1s and 0s (1001010101101101101110100010...) and then construct your bitset.

Community
  • 1
  • 1
Anthony
  • 12,177
  • 9
  • 69
  • 105
  • I referenced that example when I was building it. And does it really make a difference whether the loop iterates one way or the other? – Lady_ari Nov 22 '11 at 04:50
  • It shouldn't, but the loop in your question looks wrong. You should **not** be attempting to access `data1[i]` where `i` is equal to `data1.length()`. – Anthony Nov 22 '11 at 04:52
  • @Lady_ari: No, but it matters if you do it from 0 to n-1, or from 1 to n. – Frank Nov 22 '11 at 04:52
  • @Lady_ari: well in this case your decremental for loop starts 1 past valid memory and skips `j = 0` as the condition is `j > 0` instead of `j >= 0`. But no, that does not answer your question. – user7116 Nov 22 '11 at 04:52
  • I changed the starting value of `j` to `data1.length-1` and it didn't change anything. – Lady_ari Nov 22 '11 at 04:53
  • Will it work even if I want to concatenate the values of `data1` with what's already in `x`? – Lady_ari Nov 22 '11 at 05:12
  • Here's an example for how to concatenate one bitset to the little end of another: [link](http://pastebin.com/cguqaMgS). Not sure how efficient it is. – Anthony Nov 22 '11 at 05:22
  • I'm trying a different way...going on what you posted earlier in your edit. `for(int i=0; i x(std::string(d));` – Lady_ari Nov 22 '11 at 05:26