The C++ Standard Library
- C++ Library - <valarray>
- C++ Library - <utility>
- C++ Library - <typeinfo>
- C++ Library - <tuple>
- C++ Library - <thread>
- C++ Library - <string>
- C++ Library - <stdexcept>
- C++ Library - <regex>
- C++ Library - <numeric>
- C++ Library - <new>
- C++ Library - <memory>
- C++ Library - <locale>
- C++ Library - <limits>
- C++ Library - <functional>
- C++ Library - <exception>
- C++ Library - <complex>
- C++ Library - <atomic>
- C++ Library - <streambuf>
- C++ Library - <sstream>
- C++ Library - <ostream>
- C++ Library - <istream>
- C++ Library - <iostream>
- C++ Library - <iosfwd>
- C++ Library - <ios>
- C++ Library - <iomanip>
- C++ Library - <fstream>
- C++ Library - Home
The C++ STL Library
- C++ Library - <iterator>
- C++ Library - <algorithm>
- C++ Library - <vector>
- C++ Library - <unordered_set>
- C++ Library - <unordered_map>
- C++ Library - <stack>
- C++ Library - <set>
- C++ Library - <queue>
- C++ Library - <map>
- C++ Library - <list>
- C++ Library - <forward_list>
- C++ Library - <deque>
- C++ Library - <bitset>
- C++ Library - <array>
C++ Programming Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
C++ Library - <bitset>
Introduction
Bitset represents a fixed-size sequence of N bits and stores values either 0 or 1. Zero means value is false or bit is unset and one means value is true or bit is set. Bitset class emulates space efficient array of boolean values, where each element occupies only one bit.
As it emulates array, its index also starts from 0th position. Inspanidual bit from bitset can be accessed using subscript operator. For instance to access first element of bitset foo use foo[0].
Bitset class provides constructors to create bitset from integer as well as from strings. The size of the bitset is fixed at compile time. STL provides vector<bool> class that provides dynamic resize functionapty.
Definition
Below is definition of std::bitset from <bitset> header file
template <size_t N> class bitset;
Parameters
N − Size of the bitset.
Member types
Following member types can be used as parameters or return type by member functions.
Sr.No. | Member types | Definition |
---|---|---|
1 | reference | Proxy class that represents a reference to a bit. |
Functions from <bitset>
Below is pst of all methods from <bitset> header.
Constructors
Sr.No. | Method & Description |
---|---|
1 | Constructs bitset container and initiapze it with zero. |
2 | Constructs bitset container and initiapze it with the bit value of val. |
3 | Constructs and initiapzes a bitset container from C++ string object. |
4 | Constructs and initiapzes a bitset container from c-style string. |
Member class
Sr.No. | Method & Description |
---|---|
1 | This is embedded class which provides l-value that can be returned from std::bitset::operator[]. |
Bitset operators
Sr.No. | Method & Description |
---|---|
1 | Performs bitwise AND operation on current bitset object. |
2 | Performs bitwise OR operation on current bitset object. |
3 | Performs bitwise XOR operation on current bitset object. |
4 | Performs bitwise left SHIFT operation on current bitset object. |
5 | Performs bitwise right SHIFT operation on current bitset object. |
6 | Performs bitwise NOT operation on bitset. |
7 | Performs bitwise left SHIFT operation on bitset. |
8 | Performs bitwise right SHIFT operation on bitset. |
9 | Test whether two bitsets are equal or not. |
10 | Test whether two bitsets are equal or not. |
11 | Performs bitwise AND operation on bitset. |
12 | Performs bitwise OR operation on bitset. |
13 | Performs bitwise XOR operation on bitset. |
14 | Extracts upto N bits from is and stores into another bitset x. |
15 | Inserts bitset x to the character stream os. |
Member functions
Sr.No. | Method & Description |
---|---|
1 | Tests whether all bits from bitset are set or not. |
2 | Tests whether at least one bit from bitset is set or not. |
3 | Count number of set bits from bitset. |
4 | Toggles all bits from bitset. |
all bits
5 | Toggles single bit from bitset. |
single bit
6 | Tests whether all bits are unset or not. |
7 | Returns the value of bit at position pos. |
bool version
8 | Returns the reference of bit at position pos. |
reference version
9 | Reset all bits of bitset to zero. |
all bits
10 | Reset single bit of bitset to zero. |
single bit
11 | Set all bits from bitset to one. |
all bits
12 | Set single bit from bitset to either one or zero. |
single bit
13 | Reports the size of the bitset. |
14 | Tests whether Nth bit is set or not. |
15 | Converts bitset object to string object. |
16 | Convert bitset to unsigned long long. |
17 | Convert bitset to unsigned long. |
Non-member functions
Sr.No. | Method & Description |
---|---|
1 | Returns hash value based on the provided bitset. |