C++ Standard Library C++ STL Library

C++ <bitset> - to_ulong() Function



The C++ bitset::to_ulong function returns an unsigned long with the integer value which has the same bit representation as the given bitset.

Syntax

unsigned long to_ulong() const;
unsigned long to_ulong() const;

Parameters

No parameter is required.

Return Value

An integer value which has the same bit representation as the given bitset.

Exception

Throws overflow_error, if the bitset size is too big to be represented by the return type.

Example:

The example below shows the usage of bitset::to_ulong function.

#include <iostream>
#include <bitset>
using namespace std;
 
int main (){
  bitset<6> bset("110011");

  //converting bset into unsigned long integer
  unsigned long i = bset.to_ulong();
  cout<<bset<<" as unsigned long integer is "<<i<<".\n";

  return 0;
}

The output of the above code will be:

110011 as unsigned long integer is 51.

❮ C++ <bitset> Library