public member function
<bitset>

std::bitset::to_ullong

unsigned long long to_ullong() const;
Convert to unsigned long long
Returns an unsigned long long with the integer value that has the same bits set as the bitset.

If the bitset size is too big to be represented in a value of type unsigned long long, the function throws an exception of type overflow_error.

Parameters

none

Return value

Integer value with the same bit representation as the bitset object.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
// bitset::to_ullong
#include <iostream>       // std::cout
#include <bitset>         // std::bitset

int main ()
{
  std::bitset<4> foo;     // foo: 0000
  foo.set();              // foo: 1111

  std::cout << foo << " as an integer is: " << foo.to_ullong() << '\n';

  return 0;
}

Output:

1111 as an integer is: 15


Data races

The bitset is accessed.

Exception safety

Strong guarantee: if an exception is thrown, there are no changes in the bitset.
If the bitset size is too big to be represented by the return type, overflow_error is thrown.

See also