您的位置:首页 > 其它

Numbering conventions of strings and bitsets

2014-01-24 11:52 344 查看
转载自:C++ Primer, Fifth Edition

The indexing conventions of strings and bitsets are inversely related:The character in the string with the highest subscript (the rightmostcharacter) is used to initialize the low-order bit
in the bitset (the bit withsubscript 0). When you initialize a bitset from a string, it is essential to remember this difference.

For example:

string str("1111111000000011001101");

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

bitset<32> bitvec6(str, str.size()-4); // use last four characters

We can view these initializations as

Here bitvec5 is initialized by the substring in str starting at str[5] and continuing for four positions. As usual, the right-most character of the substring represents the lowest-order bit. Thus, bitvec5 is initialized with bit
positions 3 through 0 set to 1100 and the remaining bits set to 0. The initializer for bitvec6 passes a string and a starting point, so bitvec6 is initialized from the characters in str starting four from the end of str. The remainder of the bits in bitvec6
are initialized to zero.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: