Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Containers

C++ std::bitset class

Reference for std::bitset: a fixed-size sequence of bits with set, reset, flip, test, count, bitwise operators and conversions to integers and strings.

std::bitset<N> (header <bitset>) stores exactly N bits, where N is a compile-time constant. Each bit can be tested, set, cleared and flipped, and whole bitsets can be combined with the usual bitwise operators. Bits are packed into machine words, so a bitset<1000> takes about 125 bytes.

Bit 0 is the least significant bit, and the string form prints the most significant bit first. A bitset never changes size; when the number of bits is only known at run time, use std::vector<bool> or a hand-written structure.

C++
#include <bitset>

std::bitset<8> flags{0b00101100};

Try it

C++ Setting, testing and counting bits

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Operators, shifts and conversions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
template<std::size_t N> class bitset;

constexpr bitset() noexcept;                       // all zero
constexpr bitset(unsigned long long value) noexcept;
explicit bitset(const std::string& str, ...);      // '0' and '1' characters
explicit bitset(const char* str, ...);
Name Description
N Number of bits, fixed at compile time.
value Initial value of the low bits; higher bits of value beyond N are dropped, remaining bits are zero.
str Text of '0' and '1', most significant bit first. Any other character throws std::invalid_argument. A shorter string fills the low bits.

Members: set(pos[, value]), set(), reset(pos), reset(), flip(pos), flip(), test(pos), operator[], count, size, any, none, all (C++11), to_ulong, to_ullong, to_string, the operators & | ^ ~ << >> with their compound forms, ==, != and std::hash.

Returns: count() is the number of set bits. to_string(zero, one) returns a string with chosen characters for 0 and 1.

Errors / undefined behavior: set, reset, flip and test throw std::out_of_range if pos >= N. operator[] does not check the index (undefined behavior). to_ulong and to_ullong throw std::overflow_error if a set bit does not fit.

Usage notes

  • Indexing is from the right. In bitset<8>{"00000001"} bit 0 is set, which is the opposite of how arrays are written left to right.
  • operator[] returns a proxy for non-const bitsets, so auto x = b[0]; does not copy a bool. Write bool x = b[0]; or use test.
  • Shifts fill with zeros and never rotate; std::rotl from <bit> works on integers instead.
  • Size is a type parameter. bitset<8> and bitset<16> are different types, and you cannot resize one.
  • Good uses: feature flags, sieve of Eratosthenes flags, visited sets of small integer ranges, subset masks in dynamic programming.
  • Large bitsets should not live on the stack; bitset<10'000'000> is about 1.2 MB, so allocate it statically or with std::make_unique.
  • Stream I/O: operator<< prints N characters, and operator>> reads '0'/'1' characters until it reaches N or any other character.
  • Compared with vector<bool>: bitset is stack-friendly and fixed; vector<bool> is resizable but has no bitwise operators.