c++ bitset数组操作
时间: 2024-01-04 10:19:49 浏览: 139
以下是两个C++中使用bitset数组的例子:
1. 使用bitset数组输出二进制数
```cpp
#include <iostream>
#include <bitset>
using namespace std;
int main() {
const unsigned bit_num = 13;
bitset<bit_num> b1(0xbeef);
for (int i = bit_num - 1; i >= 0; --i) {
cout << b1[i];
}
cout << endl;
bitset<20> b2(0xbeff);
for (int i = 20 - 1; i >= 0; --i) {
cout << b2[i];
}
return 0;
}
```
2. 使用bitset数组从字符串中提取二进制数
```cpp
#include <iostream>
#include <bitset>
using namespace std;
int main() {
string bit_string = "1001001100";
bitset<10> b1(bit_string); // b1 is 1001001100
bitset<4> b2(bit_string,5, 4, '0', '1');
for (int i = 4 - 1; i >= 0; --i) {
cout << b2[i];
}
cout << endl;
bitset<4> b3(bit_string, bit_string.length() - 4);
for (int i = 4 - 1; i >= 0; --i) {
cout << b3[i];
}
return 0;
}
```
阅读全文