Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++ bitset example #22

Open
thinkerou opened this issue Aug 12, 2018 · 0 comments
Open

C++ bitset example #22

thinkerou opened this issue Aug 12, 2018 · 0 comments
Labels

Comments

@thinkerou
Copy link
Owner

#include <iostream>
#include <bitset>

using namespace std;

int main() {
	// 使用整数初始化
	bitset<3> bs(7);
	for (int i = 0; i < bs.size(); i++) {
		// bs[i]返回对应i位的值
		cout << "bs[" << i << "] is " << bs[i] << endl;
	}

	// 使用字符串初始化
	string s("011");
	bitset<3> bs1(s);
	for (int i = 0; i < bs1.size(); i++) {
		cout << "bs1[" << i << "] is " << bs1[i] << endl;
	}

	// any()方法,如果有1位为1,则返回1
	cout << "bs1.any() = " << bs1.any() << endl;

	// none()方法,如果有1位为1,则返回0,如果全为0则返回1
	bitset<3> bsNone;
	cout << "bsNone.none() = " << bsNone.none() << endl;

	// count()方法,返回几个位为1的数量
	cout << "bs1.count() = " << bs1.count() << endl;

	// size()方法,返回位数
	cout << "bs1.size() = " << bs1.size() << endl;

	// set()方法,所有位都置为1

	// reset()方法,所有位都置为0

	// flip()方法,所有位取反

	// set(pos)方法,设置位置pos的值为1

	return 0;
}
@thinkerou thinkerou added the cpp label Aug 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant