std::bitset::to_ullong and std::bitset::to_ulong in C++
Learn about two useful functions of c++ that areĀ std::bitset::to_ullong
and std::bitset::to_ulong
. Both of these functions are found in std::bitset
the header.
std::bitset
represents a sequence of bits whose size is fixed and is stored in 0,1 form. ‘0’ represents a false value or unset bit and ‘1’ represents a true value or set bit. std::bitset
have constructors that create bitset from a string as well as an integer.
std::bitset::to_ulong
std::bitset::to_ulong
converts the bitset content into an unsigned long integer.- This function takes no parameter.
- This function returns an unsigned long integer.
- Whenever an exception is thrown, bitset content remained unchanged.
Example 1
#include <bitset> #include <iostream> #include <limits> using namespace std; int main() { bitset<numeric_limits<unsigned long>::digits> p(15); // Integer input cout << p << " as an unsiged long Integer is : " << p.to_ulong() << endl;; return 0; }
Output : 0000000000000000000000000000000000000000000000000000000000001111 as an unsiged long Integer is : 15
Example 2
#include <bitset> #include <iostream> #include <limits> using namespace std; int main() { bitset<numeric_limits<unsigned long>::digits> p("1101"); //String input cout << p << "as an unsiged long Integer is : " << p.to_ulong() << endl;; return 0; }
Output : 0000000000000000000000000000000000000000000000000000000000001101 as an unsiged long Integer is : 13
std::bitset::to_ullong
std::bitset::to_ullong
converts the bitset content into an unsigned long long integer.- This function takes no parameter.
- An unsigned long long integer is returned by this function.
- Whenever an exception is thrown, bitset content remained unchanged.
Example 1
#include <bitset> #include <iostream> #include <limits> using namespace std; int main() { bitset<numeric_limits<unsigned long>::digits> p(10); //Integer Input cout << p << "as an unsiged long Integer is : " << p.to_ullong() << endl;; return 0; }
Output : 0000000000000000000000000000000000000000000000000000000000001010 as an unsiged long long Integer is : 10
Example 2
#include <bitset> #include <iostream> #include <limits> using namespace std; int main() { bitset<numeric_limits<unsigned long>::digits> p("1011"); //String Input cout << p << "as an unsiged long Integer is : " << p.to_ullong() << endl;; return 0; }
Output : 0000000000000000000000000000000000000000000000000000000000001011 as an unsiged long long Integer is : 11
Leave a Reply