C++——std::Bitset

write in front

This article systematically learns about the container bitset in the std standard library. A bitset is used to store many bits. These elements can be used to represent two states: 0 or 1, true or false…. So sometimes it is very convenient to use this container to quickly implement state storage.

The container has a very small storage space for states through special optimization of space, which is equivalent to storing several states in one bit. The element access of the container can also access the nth element in the container through [n], but since the general language does not have a bit-sized data type, a special reference type access is used here, such as: bitset::reference.

bitset class with header file includes

The bitset header file is referenced as follows:

#include <bitset>    

bitset::bitset

Construct a bitset as follows:

// 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:
//foo: 0000000000000000
//bar: 0000111110100010
//baz: 0000000101111001
}

In general, there are four ways to construct a bitset, please refer to: basic usage of bitset + experience .

Tables Are
bitset< n > b; Create an n-bit bitset b with an initial value of 0
bitset< n > b(u); Create a bitset b based on the unsigned long number u
bitset< n > b(s); Create a bitset b based on the number of bits in string s
bitset< n > b(s, pos, n); Create a bitset b of n elements starting from the pos position in string s

Bit access

write picture description here

1.bitset::operator[]
accesses a bit element and returns a value of type bool or a reference to the element. The sample code is as follows:

// bitset::operator[]
#include <iostream>       // std::cout
#include <bitset>         // std::bitset

int main ()
{
  std::bitset<4> foo;

  foo[1]=1;             // 0010
  foo[2]=foo[1];        // 0110

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

  return 0;
}

//Output:
//foo: 0110

2.bitset::count()
counts the number of bits set in the bitset, that is, the number of elements with a value of 1 in the bitset. For the total number of elements in the bitset, you can refer to: set::size(). The sample code is as follows:

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

int main ()
{
  std::bitset<8> foo (std::string("10110011"));

  std::cout << foo << " has ";
  std::cout << foo.count() << " ones and ";
  std::cout << (foo.size()-foo.count()) << " zeros.\n";

  return 0;
}

//Output:
//10110011 has 5 ones and 3 zeros.

3.bitset::size()
returns the total number of elements in the bitset. That is, the size of the bitset in the usual sense. The sample code is as follows:

// bitset::size
#include <iostream>       // std::cout
#include <bitset>         // std::bitset

int main ()
{
  std::bitset<8> foo;
  std::bitset<4> bar;

  std::cout << "foo.size() is " << foo.size() << '\n';
  std::cout << "bar.size() is " << bar.size() << '\n';

  return 0;
}

//Output:
//foo.size() is 8
//bar.size() is 4

4.bitset::test()
returns whether the element at the pos position is set, or whether it is 1. The return value is true, or false. The sample code looks like this:

// bitset::test
#include <iostream>       // std::cout
#include <string>         // std::string
#include <cstddef>        // std::size_t
#include <bitset>         // std::bitset

int main ()
{
  std::bitset<5> foo (std::string("01011"));

  std::cout << "foo contains:\n";
  std::cout << std::boolalpha;
  for (std::size_t i=0; i<foo.size(); ++i)
    std::cout << foo.test(i) << '\n';

  return 0;
}

//Output:
//foo contains:
//true
//true
//false
//true
//false

5.bitset::any()
determines whether any element is set, or whether at least one element is 1. The sample code is as follows:

// bitset::any
#include <iostream>       // std::cin, std::cout
#include <bitset>         // std::bitset

int main ()
{
  std::bitset<16> foo;

  std::cout << "Please, enter a binary number: ";
  std::cin >> foo;

  if (foo.any())
    std::cout << foo << " has " << foo.count() << " bits set.\n";
  else
    std::cout << foo << " has no bits set.\n";

  return 0;
}

//Possible output:
//Please, enter a binary number: 10110
//0000000000010110 has 3 bits set.

6.bitset::none()
determines whether a bitset has not been set. Returns false if an element in a bitset is 1, otherwise returns true. The sample code is as follows:

// bitset::none
#include <iostream>       // std::cin, std::cout
#include <bitset>         // std::bitset

int main ()
{
  std::bitset<16> foo;

  std::cout << "Please, enter a binary number: ";
  std::cin >> foo;

  if (foo.none())
    std::cout << foo << " has no bits set.\n";
  else
    std::cout << foo << " has " << foo.count() << " bits set.\n";

  return 0;
}

//Possible output:
//Please, enter a binary number: 11010111
//0000000011010111 has 6 bits set.

7.bitset::all()
judges whether all elements in a bitset are 1, if they are all 1, return true; otherwise return false; the sample code is as follows:

// bitset::all
#include <iostream>       // std::cin, std::cout, std::boolalpha
#include <bitset>         // std::bitset

int main ()
{
  std::bitset<8> foo;

  std::cout << "Please, enter an 8-bit binary number: ";
  std::cin >> foo;

  std::cout << std::boolalpha;
  std::cout << "all: " << foo.all() << '\n';
  std::cout << "any: " << foo.any() << '\n';
  std::cout << "none: " << foo.none() << '\n';

  return 0;
}

//Possible output:
//Please, enter an 8-bit binary number: 11111111
//all: true
//any: true
//none: false

Bit operations

write picture description here

1.bitset::set()
sets an element or all elements in the bitset to 1. The sample code is as follows:

// bitset::set
#include <iostream>       // std::cout
#include <bitset>         // std::bitset

int main ()
{
  std::bitset<4> foo;

  std::cout << foo.set() << '\n';       // 1111
  std::cout << foo.set(2,0) << '\n';    // 1011
  std::cout << foo.set(2) << '\n';      // 1111

  return 0;
}

//Output:
//1111
//1011
//1111

2.bitset::reset()
resets an element or all elements in the bitset to 0. The sample code is as follows:

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

int main ()
{
  std::bitset<4> foo (std::string("1011"));

  std::cout << foo.reset(1) << '\n';    // 1001
  std::cout << foo.reset() << '\n';     // 0000

  return 0;
}

//Output:
//1001
//0000

3.bitset::flip()
reverses a bitset, that is, all elements in a bitset are set to 1 and 1 to 0. The sample code is as follows:

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

int main ()
{
  std::bitset<4> foo (std::string("0001"));

  std::cout << foo.flip(2) << '\n';     // 0101
  std::cout << foo.flip() << '\n';      // 1010

  return 0;
}

//Output:
//0101
//1010

Bitset operations

write picture description here

1.bitset::to_string()
converts a bitset to string type, the sample code is as follows:

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

int main ()
{
  std::bitset<4> mybits;     // mybits: 0000
  mybits.set();              // mybits: 1111

  std::string mystring =
    mybits.to_string<char,std::string::traits_type,std::string::allocator_type>();

  std::cout << "mystring: " << mystring << '\n';

  return 0;
}

//Output:
//mystring: 1111

2.bitset::to_ulong()
converts the bitset to an unsigned_long type number. The sample code is as follows:

// bitset::to_ulong
#include <iostream>       // std::cout
#include <bitset>         // std::bitset

int main ()
{
  std::bitset<4> foo;     // foo: 0000
  foo.set();              // foo: 1111

  std::cout << foo << " as an integer is: " << foo.to_ulong() << '\n';

  return 0;
}

//Output:
//1111 as an integer is: 15

write at the end

There are so many introductions about bitset for the time being. The frequency of this container is not as high as that of other categories, but it is still necessary to know. There are some places where it can play a surprising role.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324932947&siteId=291194637