Constructors
#include <iostream>
#include <string>
#include <bitset>
using namespace std;
int main(int argc, char *argv[])
{
// 默认构造函数,所有位初始化为0
bitset<8> bs1;
bitset<16> bs2(0xfa2);
bitset<65> bs3(string("0101111001"));
cout << sizeof(bs1) << endl;
cout << sizeof(bs2) << endl;
cout << sizeof(bs3) << endl;
cout << bs1 << endl;
cout << bs2 << endl;
cout << bs3 << endl;
return 0;
}
Opeartors
#include <iostream>
#include <string>
#include <bitset>
using namespace std;
/*
!=, ==, &=, ^=, |=, ~, <<=, >>=, []
!= 返回真,如果两个bitset不相等。
== 返回真,如果两个bitset相等。
&= 完成两个bitset间的与运算。
^= 完成两个bitset间的异或运算。
|= 完成两个bitset间的或运算
~ 反置bitset (和调用 flip()类似)
<<= 把bitset向左移动
>>= 把bitset向右移动
[x] 返回第x个位的引用,从右往左
*/
int main(int argc, char *argv[])
{
bitset<4> foo (std::string("1001"));
bitset<4> bar (std::string("0011"));
cout << foo << endl;
cout << bar << endl;
// 注意此处foo的值在改变
cout << (foo ^= bar) << endl; // 1010 (XOR,assign)
cout << (foo &= bar) << endl; // 0010 (AND,assign)
cout << (foo |= bar) << endl; // 0011 (OR,assign)
cout << (foo <<= 2) << endl; // 1100 (SHL,assign)
cout << (foo >>= 1) << endl; // 0110 (SHR,assign)
cout << (~bar) << endl; // 1100 (NOT)
cout << (bar << 1) << endl; // 0110 (SHL)
cout << (bar >> 1) << endl; // 0001 (SHR)
cout << (foo == bar) << endl; // 0 (0110==0011)
cout << (foo != bar) << endl; // 1 (0110!=0011)
cout << (foo & bar) << endl; // 0010
cout << (foo | bar) << endl; // 0111
cout << (foo ^ bar) << endl; // 0101
return 0;
}
any
#include <iostream>
#include <string>
#include <bitset>
using namespace std;
/*
bool any(); 如果有位被设置为1,返回true;否则返回false
*/
int main(int argc, char *argv[])
{
bitset<4> foo (std::string("0000"));
bitset<4> bar (std::string("0001"));
cout << foo.any() << endl;
cout << bar.any() << endl;
return 0;
}
// 结果: 0 1
count
#include <iostream>
#include <string>
#include <bitset>
using namespace std;
/*
size_type count(); 返回bitset中被设置成1的位的个数
*/
int main(int argc, char *argv[])
{
bitset<4> foo (std::string("0000"));
bitset<4> bar (std::string("0101"));
cout << foo.count() << endl;
cout << bar.count() << endl;
return 0;
}
// 结果: 0 2
flip
#include <iostream>
#include <string>
#include <bitset>
using namespace std;
/*
bitset &flip(); // 反转bitset所有的位,0变为1, 1变为0
bitset &flip( size_t pos ); // 只反转pos指定的位
*/
int main(int argc, char *argv[])
{
bitset<4> foo (std::string("0000"));
bitset<4> bar (std::string("0101"));
cout << foo.flip() << endl;
cout << bar.flip(2) << endl;
return 0;
}
// 结果: 1111 0001
none
#include <iostream>
#include <string>
#include <bitset>
using namespace std;
/*
bool none(); // 如果没有位被设为1,返回ture,否则返回false
*/
int main(int argc, char *argv[])
{
bitset<4> foo (std::string("0000"));
bitset<4> bar (std::string("0101"));
cout << foo.none() << endl;
cout << bar.none() << endl;
return 0;
}
// 结果: 1 0
reset
#include <iostream>
#include <string>
#include <bitset>
using namespace std;
/*
bitset &reset(); // 将bitset全部设为0
bitset &reset( size_t pos ); // 将bitset指定的pos位设为0
*/
int main(int argc, char *argv[])
{
bitset<4> foo (std::string("0110"));
bitset<4> bar (std::string("0101"));
cout << foo.reset() << endl;
cout << bar.reset(2) << endl;
return 0;
}
// 结果: 0000 0001
set
#include <iostream>
#include <string>
#include <bitset>
using namespace std;
/*
bitset &set(); // 将bitset全部设为1
bitset &set( size_t pos, int val = 1 ); // 将bitset指定的pos位设为val(默认为1)
*/
int main(int argc, char *argv[])
{
bitset<4> foo (std::string("0110"));
bitset<4> bar (std::string("0101"));
cout << foo.set() << endl;
cout << bar.set(2, 0) << endl;
return 0;
}
// 结果: 1111 0001
size
#include <iostream>
#include <string>
#include <bitset>
using namespace std;
/*
size_t size(); // 返回bitset能容纳的位数
*/
int main(int argc, char *argv[])
{
bitset<4> foo (std::string("0110"));
bitset<65> bar (std::string("0101"));
cout << foo.size() << endl;
cout << bar.size() << endl;
return 0;
}
// 结果: 4 65
test
#include <iostream>
#include <string>
#include <bitset>
using namespace std;
/*
bool test( size_t pos ); // 返回bitset在pos位上的值
*/
int main(int argc, char *argv[])
{
bitset<4> foo (std::string("0110"));
bitset<4> bar (std::string("0101"));
cout << foo.test(1) << endl;
cout << bar.test(1) << endl;
return 0;
}
// 结果: 1 0
to_string
#include <iostream>
#include <string>
#include <bitset>
using namespace std;
/*
to_string; // 返回string形式的bitset
*/
int main(int argc, char *argv[])
{
bitset<4> foo (std::string("0110"));
foo.flip();
cout << foo.to_string() << endl;
return 0;
}
// 结果: 1001
to_ulong
#include <iostream>
#include <string>
#include <bitset>
using namespace std;
/*
unsigned long to_ulong(); // 返回无符号长整形的bitset
*/
int main(int argc, char *argv[])
{
bitset<4> foo (std::string("0010"));
unsigned long a = 1;
cout << a + foo.to_ulong() << endl;
return 0;
}
// 结果: 3