#include #include #include #include #include //for numeric_limits using namespace std; int main() { bitset<32> a; //32 bits of zeroes bitset<32> b(string("00000000111111110000000011111111")); bitset<32> c(string( //more legible way to do the same thing "00000000" "11111111" "00000000" "11111111" )); cout << "c == " << c << ", rightmost bit is " << c[0] << ".\n"; string s = c.to_string, allocator >(); cout << "s == " << s << ", leftmost bit is " << s[0] << ".\n"; if (numeric_limits::digits <= 32) { //number of bits bitset<32> d = 0xFFFF0000; unsigned long ul = d.to_ulong(); cout << "d == " << d << ", rightmost bit is " << d[0] << ".\n" << "ul == " << ul << " == " << hex << ul << dec << "\n"; } a = b & c; //can do & &= | |= ^ ^= ~ << <<= >> >>= == != cout << "a == " << a << ", rightmost bit is " << a[0] << ".\n"; a[0].flip(); //flip the rightmost bit a.flip(0); //flip the rightmost bit a.flip(); //flip all the bits cout << "a == " << a << ", rightmost bit is " << a[0] << ".\n"; if (a.none()) { cout << "None of the bits are on.\n"; } else if ((~a).none()) { cout << "All of the bits are on.\n"; } else { cout << a.count() << " of the bits are on.\n"; } a[0] = true; //Turn on the rightmost bit. a.set(0); //Turn on the rightmost bit. a.set(); //Turn on all the bits. a[0] = false; //Turn off the rightmost bit. a.reset(0); //Turn off the rightmost bit. a.reset(); //Turn off all the bits. if (a.any()) { cerr << "None of the bits should be on after a reset.\n"; } return EXIT_SUCCESS; }