---
name: "std::bitset"
section: "3cxx"
platform: "linux"
package: "libstdc++-12-doc"
version: "12.4.0-5"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/std::bitset/3cxx"
---

# std::bitset(3cxx)

The bitset class represents a fixed-size sequence of bits.

## NAME

std::bitset\< \_Nb \> - The bitset class represents a *fixed-size* sequence of bits.

## SYNOPSIS

`#include <bitset>`

Inherits **std::\_Base\_bitset\<((\_Nb)/(\_\_CHAR\_BIT\_\_ \*\_\_SIZEOF\_LONG\_\_)+((\_Nb) %(\_\_CHAR\_BIT\_\_ \*\_\_SIZEOF\_LONG\_\_)==0 ? 0 :1))\>**.

### Classes

class **reference**\

### Public Member Functions

constexpr **bitset** () noexcept\
 All bits set to zero.  template\<**typename** \_CharT \> **bitset** (const \_CharT \***\_\_str**, **typename** **std::basic\_string**\< \_CharT \>::size\_type \_\_n=**std::basic\_string**\< \_CharT \>::npos, \_CharT **\_\_zero**=\_CharT('0'), \_CharT **\_\_one**=\_CharT('1'))\
 template\<**class** \_CharT , **class** \_Traits , **class** \_Alloc \> **bitset** (const **std::basic\_string**\< \_CharT, \_Traits, \_Alloc \> &**\_\_s**, size\_t **\_\_position**, size\_t \_\_n)\
 template\<**class** \_CharT , **class** \_Traits , **class** \_Alloc \> **bitset** (const **std::basic\_string**\< \_CharT, \_Traits, \_Alloc \> &**\_\_s**, size\_t **\_\_position**, size\_t \_\_n, \_CharT **\_\_zero**, \_CharT **\_\_one**=\_CharT('1'))\
 template\<**class** \_CharT , **class** \_Traits , **class** \_Alloc \> **bitset** (const **std::basic\_string**\< \_CharT, \_Traits, \_Alloc \> &**\_\_s**, size\_t **\_\_position**=0)\
 constexpr **bitset** (unsigned long long \_\_val) noexcept\
 Initial bits bitwise-copied from a single word (others set to zero).  size\_t **\_Find\_first** () const noexcept\
 Finds the index of the first 'on' bit.  size\_t **\_Find\_next** (size\_t **\_\_prev**) const noexcept\
 Finds the index of the next 'on' bit after prev.  template\<**class** \_CharT , **class** \_Traits \> **void** **\_M\_copy\_from\_ptr** (const \_CharT \*, size\_t, size\_t, size\_t, \_CharT, \_CharT)\
 template\<**class** \_CharT , **class** \_Traits , **class** \_Alloc \> **void** **\_M\_copy\_from\_string** (const **std::basic\_string**\< \_CharT, \_Traits, \_Alloc \> &**\_\_s**, size\_t **\_\_pos**, size\_t \_\_n)\
 template\<**class** \_CharT , **class** \_Traits , **class** \_Alloc \> **void** **\_M\_copy\_from\_string** (const **std::basic\_string**\< \_CharT, \_Traits, \_Alloc \> &**\_\_s**, size\_t **\_\_pos**, size\_t \_\_n, \_CharT **\_\_zero**, \_CharT **\_\_one**)\
 template\<**class** \_CharT , **class** \_Traits , **class** \_Alloc \> **void** **\_M\_copy\_to\_string** (**std::basic\_string**\< \_CharT, \_Traits, \_Alloc \> &, \_CharT, \_CharT) const\
 template\<**class** \_CharT , **class** \_Traits , **class** \_Alloc \> **void** **\_M\_copy\_to\_string** (**std::basic\_string**\< \_CharT, \_Traits, \_Alloc \> &**\_\_s**) const\
 bool **all** () const noexcept\
 Tests whether all the bits are on.  bool **any** () const noexcept\
 Tests whether any of the bits are on.  size\_t **count** () const noexcept\
 Returns the number of bits which are set.  **bitset**\< **\_Nb** \> & **flip** () noexcept\
 Toggles every bit to its opposite value.  **bitset**\< **\_Nb** \> & **flip** (size\_t **\_\_position**)\
 Toggles a given bit to its opposite value.  bool **none** () const noexcept\
 Tests whether any of the bits are on.  **bitset**\< **\_Nb** \> **operator~** () const noexcept\
 See the no-argument flip().  **bitset**\< **\_Nb** \> & **reset** () noexcept\
 Sets every bit to false.  **bitset**\< **\_Nb** \> & **reset** (size\_t **\_\_position**)\
 Sets a given bit to false.  **bitset**\< **\_Nb** \> & **set** () noexcept\
 Sets every bit to true.  **bitset**\< **\_Nb** \> & **set** (size\_t **\_\_position**, bool \_\_val=**true**)\
 Sets a given bit to a particular value.  constexpr size\_t **size** () const noexcept\
 Returns the total number of bits.  bool **test** (size\_t **\_\_position**) const\
 Tests the value of a bit.  template\<**class** \_CharT , **class** \_Traits , **class** \_Alloc \> **std::basic\_string**\< \_CharT, \_Traits, \_Alloc \> **to\_string** () const\
 Returns a character interpretation of the bitset.  template\<**class** \_CharT , **class** \_Traits \> **std::basic\_string**\< \_CharT, \_Traits, **std::allocator**\< \_CharT \> \> **to\_string** () const\
 template\<**class** \_CharT \> **std::basic\_string**\< \_CharT, **std::char\_traits**\< \_CharT \>, **std::allocator**\< \_CharT \> \> **to\_string** () const\
 **std::basic\_string**\< char, **std::char\_traits**\< char \>, **std::allocator**\< char \> \> **to\_string** () const\
 template\<**class** \_CharT , **class** \_Traits , **class** \_Alloc \> **std::basic\_string**\< \_CharT, \_Traits, \_Alloc \> **to\_string** (\_CharT **\_\_zero**, \_CharT **\_\_one**=\_CharT('1')) const\
 template\<**class** \_CharT , **class** \_Traits \> **std::basic\_string**\< \_CharT, \_Traits, **std::allocator**\< \_CharT \> \> **to\_string** (\_CharT **\_\_zero**, \_CharT **\_\_one**=\_CharT('1')) const\
 template\<**class** \_CharT \> **std::basic\_string**\< \_CharT, **std::char\_traits**\< \_CharT \>, **std::allocator**\< \_CharT \> \> **to\_string** (\_CharT **\_\_zero**, \_CharT **\_\_one**=\_CharT('1')) const\
 **std::basic\_string**\< char, **std::char\_traits**\< char \>, **std::allocator**\< char \> \> **to\_string** (char **\_\_zero**, char **\_\_one**='1') const\
 unsigned long long **to\_ullong** () const\
 unsigned long **to\_ulong** () const\
 Returns a numerical interpretation of the bitset.

**bitset**\< **\_Nb** \> & **operator&=** (const **bitset**\< **\_Nb** \> &**\_\_rhs**) noexcept\
 **bitset**\< **\_Nb** \> & **operator\|=** (const **bitset**\< **\_Nb** \> &**\_\_rhs**) noexcept\
 **bitset**\< **\_Nb** \> & **operator^=** (const **bitset**\< **\_Nb** \> &**\_\_rhs**) noexcept\

**bitset**\< **\_Nb** \> & **operator\<\<=** (size\_t **\_\_position**) noexcept\
 **bitset**\< **\_Nb** \> & **operator\>\>=** (size\_t **\_\_position**) noexcept\

**bitset**\< **\_Nb** \> & **\_Unchecked\_set** (size\_t **\_\_pos**) noexcept\
 **bitset**\< **\_Nb** \> & **\_Unchecked\_set** (size\_t **\_\_pos**, int \_\_val) noexcept\
 **bitset**\< **\_Nb** \> & **\_Unchecked\_reset** (size\_t **\_\_pos**) noexcept\
 **bitset**\< **\_Nb** \> & **\_Unchecked\_flip** (size\_t **\_\_pos**) noexcept\
 constexpr bool **\_Unchecked\_test** (size\_t **\_\_pos**) const noexcept\

**reference** **operator\[\]** (size\_t **\_\_position**)\
 Array-indexing support.  constexpr bool **operator\[\]** (size\_t **\_\_position**) const\
 Array-indexing support.

bool **operator==** (const **bitset**\< **\_Nb** \> &**\_\_rhs**) const noexcept\
 These comparisons for equality/inequality are, well, *bitwise*.

**bitset**\< **\_Nb** \> **operator\<\<** (size\_t **\_\_position**) const noexcept\
 Self-explanatory.  **bitset**\< **\_Nb** \> **operator\>\>** (size\_t **\_\_position**) const noexcept\
 Self-explanatory.

### Friends

**class** **reference**\
 **struct** **std::hash\< bitset \>**\

## Detailed Description

### template\<size\_t \_Nb\>

class std::bitset\< \_Nb \>"The bitset class represents a *fixed-size* sequence of bits.

(Note that bitset does *not* meet the formal requirements of a `container`. Mainly, it lacks iterators.)

The template argument, *Nb*, may be any non-negative number, specifying the number of bits (e.g., '0', '12', '1024\*1024').

In the general unoptimized case, storage is allocated in word-sized blocks. Let B be the number of bits in a word, then (Nb+(B-1))/B words will be used for storage. B - NbB bits are unused. (They are the high-order bits in the highest word.) It is a class invariant that those unused bits are always zero.

If you think of bitset as *a simple array of bits*, be aware that your mental picture is reversed: a bitset behaves the same way as bits in integers do, with the bit at index 0 in the *least significant / right-hand* position, and the bit at index Nb-1 in the *most significant / left-hand* position. Thus, unlike other containers, a bitset's index *counts from right to left*, to put it very loosely.

This behavior is preserved when translating to and from strings. For example, the first line of the following program probably prints *b('a') is 0001100001* on a modern ASCII system.

```
#include <bitset>
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
    long         a = 'a';
    bitset<10>   b(a);
    cout << "b('a') is " << b << endl;
    ostringstream s;
    s << b;
    string  str = s.str();
    cout << "index 3 in the string is " << str[3] << " but\n"
         << "index 3 in the bitset is " << b[3] << endl;
}
```

Also see: https://gcc.gnu.org/onlinedocs/libstdc++/manual/ext\_containers.html for a description of extensions.

Most of the actual code isn't contained in bitset\<\> itself, but in the base class \_Base\_bitset. The base class works with whole words, not with individual bits. This allows us to specialize \_Base\_bitset for the important special case where the bitset is only a single word.

Extra confusion can result due to the fact that the storage for \_Base\_bitset *is* a regular array, and is indexed as such. This is carefully encapsulated.

## Constructor & Destructor Documentation

### template\<size\_t \_Nb\> constexpr std::bitset\< \_Nb \>::bitset ()` [inline]`, ` [constexpr]`, ` [noexcept]`

All bits set to zero.

### template\<size\_t \_Nb\> constexpr std::bitset\< \_Nb \>::bitset (unsigned long long \_\_val)` [inline]`, ` [constexpr]`, ` [noexcept]`

Initial bits bitwise-copied from a single word (others set to zero).

### template\<size\_t \_Nb\> template\<class \_CharT , class \_Traits , class \_Alloc \> std::bitset\< \_Nb \>::bitset (const std::basic\_string\< \_CharT, \_Traits, \_Alloc \> & \_\_s, size\_t \_\_position = `0`)` [inline]`, ` [explicit]`

Use a subset of a string.

**Parameters**

*\_\_s* A string of *0* and *1* characters. \
 *\_\_position* Index of the first character in *\_\_s* to use; defaults to zero.

**Exceptions**

*std::out\_of\_range* If *pos* is bigger the size of *\_\_s*. \
 *std::invalid\_argument* If a character appears in the string which is neither *0* nor *1*.

### template\<size\_t \_Nb\> template\<class \_CharT , class \_Traits , class \_Alloc \> std::bitset\< \_Nb \>::bitset (const std::basic\_string\< \_CharT, \_Traits, \_Alloc \> & \_\_s, size\_t \_\_position, size\_t \_\_n)` [inline]`

Use a subset of a string.

**Parameters**

*\_\_s* A string of *0* and *1* characters. \
 *\_\_position* Index of the first character in *\_\_s* to use. \
 *\_\_n* The number of characters to copy.

**Exceptions**

*std::out\_of\_range* If *\_\_position* is bigger the size of *\_\_s*. \
 *std::invalid\_argument* If a character appears in the string which is neither *0* nor *1*.

### template\<size\_t \_Nb\> template\<typename \_CharT \> std::bitset\< \_Nb \>::bitset (const \_CharT \* \_\_str, typename std::basic\_string\< \_CharT \>::size\_type \_\_n = std::basic\_string`<_CharT>::npos`, \_CharT \_\_zero = `_CharT('0')`, \_CharT \_\_one = `_CharT('1')`)` [inline]`, ` [explicit]`

Construct from a character array.

**Parameters**

*\_\_str* An array of characters *zero* and *one*. \
 *\_\_n* The number of characters to use. \
 *\_\_zero* The character corresponding to the value 0. \
 *\_\_one* The character corresponding to the value 1.

**Exceptions**

*std::invalid\_argument* If a character appears in the string which is neither *\_\_zero* nor *\_\_one*.

## Member Function Documentation

### template\<size\_t \_Nb\> bool std::bitset\< \_Nb \>::all () const` [inline]`, ` [noexcept]`

Tests whether all the bits are on.

**Returns**

True if all the bits are set.

### template\<size\_t \_Nb\> bool std::bitset\< \_Nb \>::any () const` [inline]`, ` [noexcept]`

Tests whether any of the bits are on.

**Returns**

True if at least one bit is set.

### template\<size\_t \_Nb\> size\_t std::bitset\< \_Nb \>::count () const` [inline]`, ` [noexcept]`

Returns the number of bits which are set.

### template\<size\_t \_Nb\> bitset\< \_Nb \> & std::bitset\< \_Nb \>::flip ()` [inline]`, ` [noexcept]`

Toggles every bit to its opposite value.

### template\<size\_t \_Nb\> bitset\< \_Nb \> & std::bitset\< \_Nb \>::flip (size\_t \_\_position)` [inline]`

Toggles a given bit to its opposite value.

**Parameters**

*\_\_position* The index of the bit.

**Exceptions**

*std::out\_of\_range* If *pos* is bigger the size of the set.

### template\<size\_t \_Nb\> bool std::bitset\< \_Nb \>::none () const` [inline]`, ` [noexcept]`

Tests whether any of the bits are on.

**Returns**

True if none of the bits are set.

### template\<size\_t \_Nb\> bitset\< \_Nb \> & std::bitset\< \_Nb \>::operator&= (const bitset\< \_Nb \> & \_\_rhs)` [inline]`, ` [noexcept]`

Operations on bitsets.

**Parameters**

*\_\_rhs* A same-sized bitset.

These should be self-explanatory.

### template\<size\_t \_Nb\> bitset\< \_Nb \> std::bitset\< \_Nb \>::operator\<\< (size\_t \_\_position) const` [inline]`, ` [noexcept]`

Self-explanatory.

### template\<size\_t \_Nb\> bitset\< \_Nb \> & std::bitset\< \_Nb \>::operator\<\<= (size\_t \_\_position)` [inline]`, ` [noexcept]`

Operations on bitsets.

**Parameters**

*\_\_position* The number of places to shift.

These should be self-explanatory.

### template\<size\_t \_Nb\> bool std::bitset\< \_Nb \>::operator== (const bitset\< \_Nb \> & \_\_rhs) const` [inline]`, ` [noexcept]`

These comparisons for equality/inequality are, well, *bitwise*.

### template\<size\_t \_Nb\> bitset\< \_Nb \> std::bitset\< \_Nb \>::operator\>\> (size\_t \_\_position) const` [inline]`, ` [noexcept]`

Self-explanatory.

### template\<size\_t \_Nb\> bitset\< \_Nb \> & std::bitset\< \_Nb \>::operator\>\>= (size\_t \_\_position)` [inline]`, ` [noexcept]`

Operations on bitsets.

**Parameters**

*\_\_position* The number of places to shift.

These should be self-explanatory.

### template\<size\_t \_Nb\> reference std::bitset\< \_Nb \>::operator\[\] (size\_t \_\_position)` [inline]`

Array-indexing support.

**Parameters**

*\_\_position* Index into the bitset.

**Returns**

A bool for a *const bitset*. For non-const bitsets, an instance of the reference proxy class.

**Note**

These operators do no range checking and throw no exceptions, as required by DR 11 to the standard.

\_GLIBCXX\_RESOLVE\_LIB\_DEFECTS Note that this implementation already resolves DR 11 (items 1 and 2), but does not do the range-checking required by that DR's resolution. -pme The DR has since been changed: range-checking is a precondition (users' responsibility), and these functions must not throw. -pme

### template\<size\_t \_Nb\> constexpr bool std::bitset\< \_Nb \>::operator\[\] (size\_t \_\_position) const` [inline]`, ` [constexpr]`

Array-indexing support.

**Parameters**

*\_\_position* Index into the bitset.

**Returns**

A bool for a *const bitset*. For non-const bitsets, an instance of the reference proxy class.

**Note**

These operators do no range checking and throw no exceptions, as required by DR 11 to the standard.

\_GLIBCXX\_RESOLVE\_LIB\_DEFECTS Note that this implementation already resolves DR 11 (items 1 and 2), but does not do the range-checking required by that DR's resolution. -pme The DR has since been changed: range-checking is a precondition (users' responsibility), and these functions must not throw. -pme

### template\<size\_t \_Nb\> bitset\< \_Nb \> & std::bitset\< \_Nb \>::operator^= (const bitset\< \_Nb \> & \_\_rhs)` [inline]`, ` [noexcept]`

Operations on bitsets.

**Parameters**

*\_\_rhs* A same-sized bitset.

These should be self-explanatory.

### template\<size\_t \_Nb\> bitset\< \_Nb \> & std::bitset\< \_Nb \>::operator\|= (const bitset\< \_Nb \> & \_\_rhs)` [inline]`, ` [noexcept]`

Operations on bitsets.

**Parameters**

*\_\_rhs* A same-sized bitset.

These should be self-explanatory.

### template\<size\_t \_Nb\> bitset\< \_Nb \> std::bitset\< \_Nb \>::operator~ () const` [inline]`, ` [noexcept]`

See the no-argument flip().

### template\<size\_t \_Nb\> bitset\< \_Nb \> & std::bitset\< \_Nb \>::reset ()` [inline]`, ` [noexcept]`

Sets every bit to false.

### template\<size\_t \_Nb\> bitset\< \_Nb \> & std::bitset\< \_Nb \>::reset (size\_t \_\_position)` [inline]`

Sets a given bit to false.

**Parameters**

*\_\_position* The index of the bit.

**Exceptions**

*std::out\_of\_range* If *pos* is bigger the size of the set.

Same as writing `set(pos,false)`.

### template\<size\_t \_Nb\> bitset\< \_Nb \> & std::bitset\< \_Nb \>::set ()` [inline]`, ` [noexcept]`

Sets every bit to true.

### template\<size\_t \_Nb\> bitset\< \_Nb \> & std::bitset\< \_Nb \>::set (size\_t \_\_position, bool \_\_val = true)` [inline]`

Sets a given bit to a particular value.

**Parameters**

*\_\_position* The index of the bit. \
 *\_\_val* Either true or false, defaults to true.

**Exceptions**

*std::out\_of\_range* If *pos* is bigger the size of the set.

### template\<size\_t \_Nb\> constexpr size\_t std::bitset\< \_Nb \>::size () const` [inline]`, ` [constexpr]`, ` [noexcept]`

Returns the total number of bits.

### template\<size\_t \_Nb\> bool std::bitset\< \_Nb \>::test (size\_t \_\_position) const` [inline]`

Tests the value of a bit.

**Parameters**

*\_\_position* The index of a bit.

**Returns**

The value at *pos*.

**Exceptions**

*std::out\_of\_range* If *pos* is bigger the size of the set.

### template\<size\_t \_Nb\> template\<class \_CharT , class \_Traits , class \_Alloc \> std::basic\_string\< \_CharT, \_Traits, \_Alloc \> std::bitset\< \_Nb \>::to\_string () const` [inline]`

Returns a character interpretation of the bitset.

**Returns**

The string equivalent of the bits.

Note the ordering of the bits: decreasing character positions correspond to increasing bit positions (see the main class notes for an example).

### template\<size\_t \_Nb\> unsigned long std::bitset\< \_Nb \>::to\_ulong () const` [inline]`

Returns a numerical interpretation of the bitset.

**Returns**

The integral equivalent of the bits.

**Exceptions**

*std::overflow\_error* If there are too many bits to be represented in an `unsigned` `long`.

## Author

Generated automatically by Doxygen for libstdc++ from the source code.
