跳到正文

个人记录

一份代码学会C++中的bitset

hlb44约 7 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年4月21日
篇幅
约 7 分钟

版本

  1. v2当前
  2. v1
C++
#include<bits/stdc++.h>
using namespace std;
#define fo(i,begin,end) for(int i=begin;i<=end;i++)
/*
bitset<len> name(string(""));
将name的从右往左第pos+1位赋值为val
name[pos]=val;
将name左移:
a<<1 返回类型为 bitset,越界自动去除
a<<=1 等价于 a=a<<1;
将name右移:
a>>1 返回类型为 bitset,越界自动去除。
a>>=1 等价于 a=a>>1;
将name和另一个bitset类型name2进行操作:略。
将bitset类型的name转化为string/ull/int型值。
将name全清 0:name.reset();
将name全清 1:name.set();
将name的第pos位变为val,若val为1,可以不写val。
获取name的第pos位:name.test(pos);
name是否有至少一位的值为1:name.any(),有返回 1,没有返回 0。
name是否全为0:name.none(),是返回 1,否则返回 0。
name中 1 的个数:name.count();
name中的元素个数:name.size();
name中 0 的个数:name.size()-name.count();
将name中的某些元素取反:name.flip();,默认全部取反,填入pos参数即让pos这个位置的元素取反
*/
int main(){
	int n=12;
	string s;
	while(n) s+=n%2+'0',n/=2;//字符串存储n的二进制
	reverse(s.begin(),s.end());//翻转字符串

	bitset<8> a(s);//从右往左存储
	cout<<a<<"\n";//输出00001100

	a[0]=1;//索引从右边开始,右数第一位索引为0
	cout<<a<<"\n";//输出  00001101

	a<<=1;
	cout<<a<<"\n";//输出  00011010

	a<<=4;
	cout<<a<<"\n";//输出  10100000

	a>>=1;
	cout<<a<<"\n";//输出  01010000

	a=a&bitset<8>(string("01000000"));
	cout<<a<<"\n";//输出  01000000

	a=a^bitset<8>(string("00100011"));
	cout<<a<<"\n";//输出  01100011

	a=a|bitset<8>(string("00100101"));
	cout<<a<<"\n";//输出  01100111

	auto x=bitset<8>(string("00001100"));
	auto val=a.to_ullong();
	cout<<val<<"\n";//输出 12

	auto str=a.to_string();
	cout<<str<<"\n";//输出 01100111

	x.reset();//x为00000000
	cout<<x.to_ulong()<<"\n";//输出 0

	x.set();//x为11111111
	cout<<x.to_ulong()<<"\n";//输出 255

	x.set(0,0);//x为11111110
	cout<<x.to_ulong()<<"\n";//输出 254

	x.reset();//x为00000000
	x.set(1);//x为00000010
	cout<<x.to_ulong()<<"\n";//输出 2

	val=x.test(1);//x为00000010
	cout<<val<<"\n";//输出 1

	x.reset();//x为00000000
	cout<<(bool)(x.any())<<"\n";//输出 0
	x[0]=1;//x为00000001
	cout<<(bool)(x.any())<<"\n";//输出 1
	x.set();//x为11111111
	cout<<(bool)(x.any())<<"\n";//输出 1

	x.reset();//x为00000000
	cout<<(bool)(x.none())<<"\n";//输出 1
	x[0]=1;//x为00000001
	cout<<(bool)(x.none())<<"\n";//输出 0

	x.set();//x为11111111
	x[0]=0;//x为11111110
	x[1]=0;//x为11111100
	cout<<x.count()<<"\n";//输出 6
	cout<<x.size()-x.count()<<"\n";//输出 2

	x.flip();//x为00000011
	cout<<x.count()<<"\n";//输出 2
	cout<<x.size()-x.count()<<"\n";//输出 6

	x.flip(0);//x为00000010
	cout<<x.count()<<"\n";//输出1
	cout<<x.size()-x.count()<<"\n";//输出7
	return 0;
}

请在完全理解后,拿出一张纸,看看输出内容分别是什么。最后有答案,一共26个输出,你能对几个?

C++
#include<bits/stdc++.h>
using namespace std;
int main(){
	int n=12;
	string s;
	while(n) s+=n%2+'0',n/=2;
	reverse(s.begin(),s.end());
	bitset<8> a(s);
	cout<<a<<"\n";
	a[0]=1;
	cout<<a<<"\n";
	a<<=1;
	cout<<a<<"\n";
	a<<=4;
	cout<<a<<"\n";
	a>>=1;
	cout<<a<<"\n";
	a=a&bitset<8>(string("01000000"));
	cout<<a<<"\n";
	a=a^bitset<8>(string("00100011"));
	cout<<a<<"\n";
	a=a|bitset<8>(string("00100101"));
	cout<<a<<"\n";
	auto x=bitset<8>(string("00001100"));
	auto val=a.to_ullong();
	cout<<val<<"\n";
	auto str=a.to_string();
	cout<<str<<"\n";
	x.reset();
	cout<<x.to_ulong()<<"\n";
	x.set();
	cout<<x.to_ulong()<<"\n";
	x.set(0,0);
	cout<<x.to_ulong()<<"\n";
	x.reset();
	x.set(1);
	cout<<x.to_ulong()<<"\n";
	val=x.test(1);
	cout<<val<<"\n";
	x.reset();
	cout<<(bool)(x.any())<<"\n";
	x[0]=1;
	cout<<(bool)(x.any())<<"\n";
	x.set();
	cout<<(bool)(x.any())<<"\n";
	x.reset();
	cout<<(bool)(x.none())<<"\n";
	x[0]=1;
	cout<<(bool)(x.none())<<"\n";
	x.set();
	x[0]=0;
	x[1]=0;
	cout<<x.count()<<"\n";
	cout<<x.size()-x.count()<<"\n";
	x.flip();
	cout<<x.count()<<"\n";
	cout<<x.size()-x.count()<<"\n";
	x.flip(0);
	cout<<x.count()<<"\n";
	cout<<x.size()-x.count()<<"\n";
	return 0;
}

正确答案:

代码
00001100
00001101
00011010
10100000
01010000
01000000
01100011
01100111
103
01100111
0
255
254
2
1
0
1
1
1
0
6
2
2
6
1
7
原文