std::bitset::to_ullong and std::bitset::to_ulong in C++ STL

Bitset: A bitset is an array of bool but each Boolean value is not stored separately instead bitset optimizes the space such that each bool takes 1 bit space only, so space taken by bitset bs is less than that of bool bs[N] and vector bs(N). However, a limitation of bitset is, N must be known at compile time, i.e., a constant (this limitation is not there with vector and dynamic array)

std::bitset::to_ullong

This function Converts the contents of the bitset to an unsigned long long integer. The first bit of the bitset corresponds to the least significant digit of the number and the last bit corresponds to the most significant digit.



Syntax:

bit.to_ullong()

Here bit is a number in bits (i.e., 101 for 5)

    Parameters:

  • We are not passing any parameters
  • Return:

  • Return the converted integer.
  • Exceptions:

  • overflow_error will occur if the value can not be represented in unsigned long long.

Examples:

Input : 1010
Output : 10
Input : 10000001
Output :129

# CODE 1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <bitset> 
#include <iostream> 
#include <limits> 
using namespace std; 
  
int main() 
    bitset<numeric_limits<unsigned long long>::digits> b(10); 
  
    cout << b << endl << b.to_ullong();
  
    return 0; 
}
  
//Code is improved by Rajnis09

chevron_right


OUTPUT:

0000000000000000000000000000000000000000000000000000000000001010  
10

# CODE 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <bitset>
#include <iostream>
#include <limits>
using namespace std;
  
int main()
{
    bitset<numeric_limits<unsigned long long>::digits> b(20);
  
    cout << b << endl << b.to_ullong();
  
    return 0;
}

chevron_right


OUTPUT:


0000000000000000000000000000000000000000000000000000000000010100 
20

std::bitset::to_ulong

This function Converts the contents of the bitset to an unsigned long integer. The first bit of the bitset corresponds to the least significant digit of the number and the last bit corresponds to the most significant digit.

Syntax:

bit.to_ulong()

Here bit is a number in bits (i.e., 101 for 5)

Parameters:

  • We are not passing any parameters
  • Return:

  • Return the converted integer.
  • Exceptions associated:

  • overflow_error will occur if the value can not be represented in unsigned long.
  • Examples:

    Input : 1010
    
    Output : 10
    
    Input : 10000001
    Output :129
    

    # CODE 1 :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <bitset>
    #include <iostream>
    #include <limits>
    using namespace std;
      
    int main()
    {
        bitset<numeric_limits<unsigned long>::digits> b(10);
      
        cout << b << endl << b.to_ulong();
      
        return 0;
    }

    chevron_right

    
    

    OUTPUT:

    0000000000000000000000000000000000000000000000000000000000001010  
    10
    

    # CODE 2 :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <bitset>
    #include <iostream>
    #include <limits>
    using namespace std;
      
    int main()
    {
        bitset<numeric_limits<unsigned long>::digits> b(20);
      
        cout << b << endl << b.to_ulong();
      
        return 0;
    }

    chevron_right

    
    

    OUTPUT:

    0000000000000000000000000000000000000000000000000000000000010100 
    20
    


    My Personal Notes arrow_drop_up

    pawanasipugmailcom

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



    Improved By : Rajnis09



    Article Tags :
    Practice Tags :


    1


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.