Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library


C++ <bitset>

Bitset is a fixed-size sequence container which contains elements with only two possible values: 0 or 1. Zero means value is false (bit is unset) and one means value is true (bit is set).

Bitset class emulates space efficient array of bool elements. Along with this, each bit position can be accessed individually using subscript operator and index number which starts with 0th position. For example: first element of bitset bset can be accessed using bset[0].

Bitset class has the feature to construct bitsets from both integer values and binary strings. The size of a bitset is fixed at compile-time. STL provides vector<bool> class that provides dynamic resize functionality.

Syntax

template <size_t N> class bitset;

Parameters

N Size of the bitset in terms of number of bits.

Member types

FunctionsDescription
reference Proxy class that represents a reference to a bit.

C++ - Bitset Functions

The C++ bitset container has a number of member functions which are listed below:

FunctionsDescription
operator&= Perform bitwise AND operation on current bitset object.
operator|= Perform bitwise OR operation on current bitset object.
operator^= Perform bitwise XOR operation on current bitset object.
operator<<= Perform bitwise left SHIFT operation on current bitset object.
operator>>= Perform bitwise right SHIFT operation on current bitset object.
operator~ Perform bitwise NOT operation on the given bitset.
operator<< Perform bitwise left SHIFT operation on the given bitset.
operator>> Perform bitwise right SHIFT operation on the given bitset.
operator== Check if two bitsets are equal or not.
operator!= Check if two bitsets are equal or not.
operator& Perform bitwise AND operation on the given bitset.
operator| Perform bitwise OR operation on the given bitset.
operator^ Perform bitwise XOR operation on the given bitset.
operator>> (extractor) Extract up to N bits from basic_istream and store into another bitset.
operator<< (inserter) Insert bitset to the character stream.

Bit Access

FunctionsDescription
operator[]() Access bit of the bitset.
count() Return the number of bits in the bitset which are set.
size() Return the number of bits in the bitset.
test() Check if the bit at specified position is set.
any() Check if any bit of the bitset is set.
none() Check if no bit of the bitset is set.
all() Check if all bits of the bitset are set.

Bit operations

FunctionsDescription
set() Set the bits.
reset() Reset the bits.
flip() Flip the bits.

Bitset operations

FunctionsDescription
to_ulong() Convert the bitset to unsigned long integer
to_ullong() Convert the bitset to unsigned long long integer