public member function
<bitset>

std::bitset::to_ulong

unsigned long to_ulong() const;
Convert to unsigned long integer
Returns an unsigned 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, 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_ulong
#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_ulong() << '\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