C++关于字符串 Bitset 操作的新手
我目前正在学习位集,其中有一段介绍了它们与字符串的交互:
“字符串和位集的编号约定成反比:字符串中最右边的字符 - 具有最高下标的字符-- 用于初始化位集中的低位 - 下标为 0 的位。"
但是后来他们给出了一个示例+图表,其中显示了这样的内容:
string str("1111111000000011001101");
bitset<32> bitvec5(str, 5, 4); // 4 bits starting at str[5], 1100
str
:
1 1 1 1 1 (1 1 0 0) 0 0 0 ...
bitvec5
的值:
...0 0 0 0 0 0 0 (1 1 0 0)
此示例显示它获取最右边的位并将其放入,以便字符串中的最后一个元素是位集中的最后一个元素,而不是第一个。
哪个是对的?(或者都是错的?)
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?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
他们都是对的。
传统上,机器字中的位是从右到左编号的,因此最低位(位 0)位于右侧,就像在字符串中一样。
位集看起来像这样
,看起来相同的字符串
将具有
string[0] == '1'
和string[3] == '0'
,确切地说对面的!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
and the string that looks the same
will have
string[0] == '1'
andstring[3] == '0'
, the exact opposite!因此,您正在阅读的任何内容都是正确的(文本和示例):
So whatever you are reading is correct(both text and example) :