std::bitset::to_ullong and std::bitset::to_ulong in C++ STL
Last Updated :
13 Aug, 2019
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 :
CPP
#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 :
CPP
#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 :
CPP
#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 :
CPP
#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::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 represen
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++.Exa
4 min read
Difference between long int and long long int in C/C++ All variables use data type during declarations to restrict the type of data to be stored. Therefore, we can say that data types are used to tell the variables the type of data it can store. Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data
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 st
3 min read
Count number of set bits in a range using bitset Given a large binary number.The task is to count the number of 1's in a given range from L to R (1 based indexing).Examples: Input : s = "101101011010100000111", L = 6, R = 15 Output : 5 s [L : R] = "1011010100" There is only 5 set bits.Input : s = "10110", L = 2, R = 5 Output : 2 Approach: Convert
5 min read