Open In App

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

Last Updated : 13 Aug, 2019
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 :




#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


OUTPUT:

0000000000000000000000000000000000000000000000000000000000001010  
10

# CODE 2 :




#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;
}


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 :




    #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;
    }

    
    

    OUTPUT:

    0000000000000000000000000000000000000000000000000000000000001010  
    10
    

    # CODE 2 :




    #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;
    }

    
    

    OUTPUT:

    0000000000000000000000000000000000000000000000000000000000010100 
    20
    


    Similar Reads

    std::fixed, std::scientific, std::hexfloat, std::defaultfloat in C++
    Formatting in the standard C++ libraries is done through the use of manipulators, special variables or objects that are placed on the output stream. There are two types of floating point manipulators namely, fixed floating point and scientific floating point. These all are defined in header <iostream>. Use of precision : In the default floati
    3 min read
    std::string::length, std::string::capacity, std::string::size in C++ STL
    Prerequisite: String in C++ String class is one of the features provided by the Standard template library to us, So it comes up with great functionality associated with it. With these Functionalities, we can perform many tasks easily. Let's see a few of the functionalities string class provides. Header File <string> String Functionalities The
    6 min read
    std::oct , std::dec and std::hex in C++
    This function is used to set the base to octal, decimal or hexadecimal. It sets the basefield format flag for the str stream to the specified base std::oct : When basefield is set to octal, integer values inserted into the stream are expressed in octal base (i.e., radix 8). For input streams, extracted values are also expected to be expressed in oc
    2 min read
    std::istream_iterator and std::ostream_iterator in C++ STL
    The STL is a very powerful library in C++. It is strongly built on the principles of template programming. The STL library has three main components : Containers: These classes define the data structures which are used to contain the data. The data may be stored in linked lists, or trees or arrays. The containers provided in the STL are vector, deq
    6 min read
    std::minmax() and std::minmax_element() in C++ STL
    C++ defined functions to get smallest and largest elements among 2 or in a container using different functions. But there are also functions that are used to get both smallest and largest element using a single function, "minmax()" function achieves this task for us. This function is defined in "algorithm" header file. This article would deal in it
    4 min read
    std::upper_bound and std::lower_bound for Vector in C++ STL
    Click here for Set 1 and Set 2 of Vectors. Vector - upper_bound and lower_boundIterator lower_bound (Iterator first, Iterator last, const val) lower_bound returns an iterator pointing to the first element in the range [first,last) which has a value not less than 'val' and if the value is not present in the vector then it returns the end iterator. I
    4 min read
    std::stod, std::stof, std::stold in C++
    std::stod() : It convert string into double. Syntax: double stod( const std::string& str, std::size_t* pos = 0 ); double stod( const std::wstring& str, std::size_t* pos = 0 ); Return Value: return a value of type double Parameters str : the string to convert pos : address of an integer to store the number of characters processed. This param
    3 min read
    std::setbase, std::setw , std::setfill in C++
    The useful input/output manipulators are std::setbase, std::setw and std::setfill. These are defined in and are quite useful functions. std::base : Set basefield flag; Sets the base-field to one of its possible values: dec, hex or oct according to argument base.Syntax : std::setbase (int base);decimal : if base is 10hexadecimal : if base is 16octal
    3 min read
    std::rotate vs std::rotate_copy in C++ STL
    rotate in STL:It rotates the order of the elements in the range [first, last), in such a way that the element pointed by middle becomes the new first element, i, e, to the left. // Illustrating the use of rotate algorithm #include <bits/stdc++.h> using namespace std; // Driver Program int main() { vector<int> arr; // set some values: 1
    2 min read
    Arithmetic operations with std::bitset in C++
    A bitset is an array of boolean values, 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 say, bs is less than that of bool bs[N] and vector<bool> bs(N). However, a limitation of bitset is, N must be known at compile-time, i.e., a constant
    3 min read
    bitset size() in C++ STL
    bitset::size() is a built-in STL in C++ which returns the total number of bits. Syntax: bitset_name.size() Parameter: The function accepts no parameter. Return Value: The function returns an integral value which signifies the number of bits. It eventually returns the size that has been given while initializing the bitset. Below programs illustrate
    2 min read
    bitset::flip() in C++ STL
    bitset::flip() is a built-in STL in C++ which flips the bits. If no parameter is passed in the function, then it flips all the bit values converting zeros to ones and ones to zeros. If a parameter position is passed, it flips the bit at the position only. Syntax: bitset_name.flip(int pos) Parameter: The function accepts a parameter pos which is not
    2 min read
    bitset none() in C++ STL
    bitset::none() is a built-in STL in C++ which returns True if none of its bits are set. It returns False if a minimum of one bit is set. Syntax: bool none() Parameter: The function accepts no parameter. Return Value: The function returns a boolean. The boolean value is True if none of its bits are set. It is False if at least one bit is set. Below
    2 min read
    bitset any() in C++ STL
    The bitset::any() is an inbuilt function in C++ STL which returns True if at least one bit is set in a number. It returns False if all the bits are not set or if the number is zero. Syntax: bool any() Parameter: The function does not accepts any parameter. Return Value: The function returns a boolean value. The boolean value thus returned is True i
    2 min read
    bitset all() function in C++ STL
    The bitset::all() is a built-in function in C++ STL which returns True if all bits are set in the binary representation of a number if it is initialized in the bitset. It returns False if all the bits are not set. Syntax: bool bitset_name.all() Parameter: This function does not accepts any parameter. Return Value: The function returns a boolean val
    2 min read
    bitset set() function in C++ STL
    bitset::set() is a built-in STL in C++ which sets the bit to a given value at a particular index. If no parameter is passed, it sets all bits to 1. If only a single parameter is passed, it sets the bit at that particular index to 1. Syntax: set(int index, bool val) Parameter: The function accepts two parameters which are described below: index - th
    2 min read
    bitset operator[] in C++ STL
    bitset::operator[] is a built-in function in C++ STL which is used to assign value to any index of a bitset. Syntax: bitset_operator[index] = value Parameter: The parameter index specifies the position at which the value is to be assigned. Return Value: The function returns the value to the bit at the index. Below programs illustrate the bitset::op
    2 min read
    bitset reset() function in C++ STL
    bitset::reset() is a built-in function in C++ STL which resets the bits at the given index in the parameter. If no parameter is passed then all the bits are reset to zero. Syntax: reset(int index) Parameter: The function accepts a parameter index which signifies the position at which the bit has to be reset to zero. If no parameter is passed, all t
    2 min read
    Difference between std::swap and std::vector::swap
    The std::swap is a general function used to exchange the given values whereas the std::vector::swap is a specialized function that can swap all the contents of two different vector containers.Below are some major key differences between std::swap and std::vector::swap, std::swapstd::vector::swapThe std::swap() is a built-in function in C++ STL whic
    3 min read
    Difference between std::quick_exit and std::abort
    std::quick_exit() It Causes normal program termination to occur without completely cleaning the resources. Syntax : void quick_exit(int exit_code) no except; In case of execution of threads the codes becomes complex and knowing the execution of threads is hard. While one the thread might be waiting for a process to end while the other thread is wai
    3 min read
    Difference between std::set::lower_bound and std::lower_bound in C++
    Prerequisite: Random-access Iterators in C++, Bidirectional Iterators in C++. std::lower_bound in C++: The lower_bound() method in C++ is used to return an iterator pointing to the first element in the range [first, last) which has a value not less than the given value. This means that the function returns the index of the next smallest number just
    4 min read
    Difference between std::set::upper_bound and std::upper_bound in C++
    Prerequisites: Random-access Iterators, Bidirectional Iterators Sets are a type of associative container in which each element has to be unique because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. Functions
    4 min read
    std::tuple_element() and std::tuple_size() in C++ with Examples
    A tuple is an object that can hold a number of elements. The elements can be different data types. The elements of tuples are initialized as arguments in the order in which they will be accessed. The functions- tuple_element() and tuple_size() are only defined for elements using tuple_like interface. tuple_element():The C++ function tuple_element(a
    2 min read
    std::tuple, std::pair | Returning multiple values from a function using Tuple and Pair in C++
    There can be some instances where you need to return multiple values (maybe of different data types ) while solving a problem. One method to do the same is by using pointers, structures or global variables, already discussed here There is another interesting method to do the same without using the above methods,  using tuples (for returning multipl
    2 min read
    std::replace and std::replace_if in C++
    std::replace Assigns new_value to all the elements in the range [first, last) that compare to old_value. The function use operator == to compare the individual elements to old_value Function Template : void replace (ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value) first, last : Forward iterators to the in
    4 min read
    std::stoul and std::stoull in C++
    std::stoul Convert string to unsigned integer. Parses str interpreting its content as an integral number of the specified base, which is returned as an unsigned long value. unsigned long stoul (const string& str, size_t* idx = 0, int base = 10); Parameters : str : String object with the representation of an integral number. idx : Pointer to an
    3 min read
    std::stol() and std::stoll() Functions in C++
    In C++, std::stol() and std::stoll() are the library functions used to convert the given string to integer value of type long int and long long int respectively. They are defined inside <string> header file. In this article, we will learn about std::stol() and std::stoll() functions in C++. Example: [GFGTABS] C++ // C++ program to illustrate
    4 min read
    Find and print duplicate words in std::vector<string> using STL functions
    Consider an array of string and find duplicate words in that array and print that duplicate words if exist.Examples: Input : { "welcome", "to", "geeks", "for", "geeks" } Output : geeks Input : { "reduce", "reuse", "recycle", "reduce", "reuse", "recycle", " recycle" } Output : recycle reduce reuse Input : { "Go", "Green" } Output : No Duplicate word
    2 min read
    std::string::remove_copy(), std::string::remove_copy_if() in C++
    remove_copy() It is an STL function in c++ which is defined in algorithm library. It copies the elements in the range [first, last) to the range beginning at result, except those elements that compare equal to given elements. The resulting range is shorter than [first,last) by as many elements as matches in the sequence, which are "removed". The re
    4 min read
    std::regex_match, std::regex_replace() | Regex (Regular Expression) In C++
    Regex is the short form for “Regular expression”, which is often used in this way in programming languages and many different libraries. It is supported in C++11 onward compilers.Function Templates used in regex regex_match() -This function return true if the regular expression is a match against the given string otherwise it returns false. C/C++ C
    3 min read
    Article Tags :
    Practice Tags :
    three90RightbarBannerImg