gpt4 book ai didi

c++ - 如何从二进制字符串创建位集?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:27:51 26 4
gpt4 key购买 nike

我在文件中有一个二进制字符串,看起来像

0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001100100101101100110000110100110110111000010100110110110110111000011001000010110010010001100010010001010010010100001011001100010100001100100011011101

(256 位)。我可以将此字符串设置为 bitset<256> 的值吗?真的很快吗?

目前我正在做

for (int t = sizeof(c) - 1; t > 0; t--) {
if (c[t] == '1') {
b |= 1;
}
b <<= 1;
}
b >>= 1;

但我的结果不正确。

最佳答案

Can I set this string as the value of a bitset<256> really quickly?

是的,您可以只使用 constructor ,下面的示例直接取自文档并且也适用于 256:

std::string bit_string = "110010";
std::bitset<8> b3(bit_string); // [0,0,1,1,0,0,1,0]

std::cout << b3.to_string() << std::endl ;

关于c++ - 如何从二进制字符串创建位集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17129365/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com