Based on C++ bitset common functions and operators of

  • 2020-06-01 10:25:00
  • OfStack

C++ bitset -- STL is a must for high end bit CARDS

------------------------------------------------------------

The following content is translated from cplusplus.com, which has greatly improved my English ability.

bitset stores the base 2 digits.

bitset is like an array of type 1 of bool, but it is spatially optimized -- one element in bitset takes up only 1 bit, or 1/8 of the space of one char element.

Each element in bitset can be accessed separately. For example, for an bitset called foo, the expression foo[3] accesses its fourth element, just like array 1.

bitset has one feature: both integer types and Boolean arrays can be converted to bitset.

The size of bitset needs to be determined at compile time. If you want an unspecified length bitset, use the (weird) vector < bool > .

Define 1 bitset


// constructing bitsets
#include <iostream>  // std::cout
#include <string>   // std::string
#include <bitset>   // std::bitset

int main ()
{
 std::bitset<16> foo;
 std::bitset<16> bar (0xfa2);
 std::bitset<16> baz (std::string("0101111001"));

 std::cout << "foo: " << foo << '\n';
 std::cout << "bar: " << bar << '\n';
 std::cout << "baz: " << baz << '\n';

 return 0;
}

Output results:


foo: 0000000000000000
bar: 0000111110100010
baz: 0000000101111001

bitset operations

The operation of bitset is like a normal integer 1, which can be performed with ( & ), or (|), xor (^), left shift ( < < ), right shift ( > > ) and so on.


// bitset operators
#include <iostream>  // std::cout
#include <string>   // std::string
#include <bitset>   // std::bitset

int main ()
{
 std::bitset<4> foo (std::string("1001"));
 std::bitset<4> bar (std::string("0011"));

 std::cout << (foo^=bar) << '\n';  // 1010 (XOR,assign)
 std::cout << (foo&=bar) << '\n';  // 0010 (AND,assign)
 std::cout << (foo|=bar) << '\n';  // 0011 (OR,assign)

 std::cout << (foo<<=2) << '\n';  // 1100 (SHL,assign)
 std::cout << (foo>>=1) << '\n';  // 0110 (SHR,assign)

 std::cout << (~bar) << '\n';   // 1100 (NOT)
 std::cout << (bar<<1) << '\n';   // 0110 (SHL)
 std::cout << (bar>>1) << '\n';   // 0001 (SHR)

 std::cout << (foo==bar) << '\n';  // false (0110==0011)
 std::cout << (foo!=bar) << '\n';  // true (0110!=0011)

 std::cout << (foo&bar) << '\n';  // 0010
 std::cout << (foo|bar) << '\n';  // 0111
 std::cout << (foo^bar) << '\n';  // 0101

 return 0;
}

See the comments for the output of the code above. (note that this code involves an assignment operation)

The correlation function of bitset

For 1 bitset called foo:

foo.size() returns size (bits)
foo.count () returns the number of 1's
foo.any () returns whether there is a 1
foo.none () returns whether there is no 1
foo.set () all becomes 1
foo.set (p) turns the p + 1 bit into a 1
foo.set(p, x) turns the p + 1 bit into x
foo.reset() all becomes 0
foo.reset(p) turns the p + 1 bit into a 0
foo.flip() is all inverted
foo. flip(p) inverts the p + 1 bit
foo.to_ulong () returns the result of its conversion to unsigned long, reporting an error if out of range
foo.to_ullong () returns the result of its conversion to unsigned long long, reporting an error if out of range
foo.to_string () returns the result of its conversion to string


Related articles: