2

I'm currently learning about bitset, and in one paragraph it says this about their interactions with strings:

"The numbering conventions of strings and bitsets are inversely related: the rightmost character in the string--the one with the highest subscript--is used to initialize the low order bit in the bitset--the bit with subscript 0."

however later on they give an example + diagram which shows something like this:

string str("1111111000000011001101");
bitset<32> bitvec5(str, 5, 4); // 4 bits starting at str[5], 1100

value of str:
1 1 1 1 1 (1 1 0 0) 0 0 0 ...

value of bitvec5:
...0 0 0 0 0 0 0 (1 1 0 0)

This example shows it taking the rightmost bit and putting it so the last element from the string is the last in the bitset, not the first.

Which is right?(or are both wrong?)

4

2 に答える 2

3

They are both right.

Traditionally the bits in a machine word are numbered from right to left, so the lowest bit (bit 0) is to the right, just like it is in the string.

The bitset looks like this

...1100   value
...3210   bit numbers

and the string that looks the same

"1100"

will have string[0] == '1' and string[3] == '0', the exact opposite!

于 2011-07-27T09:44:08.273 に答える
0
string strval("1100");        //1100, so from rightmost to leftmost : 0 0 1 1
bitset<32> bitvec4(strval);   //bitvec4 is 0 0 1 1 

So whatever you are reading is correct(both text and example) :

the rightmost character in the string--the one with the highest subscript--is used to initialize the low order bit in the bitset--the bit with subscript 0.

于 2011-07-27T09:42:11.950 に答える