Tuesday, 19 June 2018

bitset

A bitset stores bits:






#include          // 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;
}

------------
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";

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

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

------------
std::bitset<4> foo (std::string("1011"));

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

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


No comments:

Post a Comment