|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member types | ||||
| Member functions | ||||
(until C++20) | ||||
| Element access | ||||
| Capacity | ||||
| Modifiers | ||||
| Conversions | ||||
bitset::to_ulong | ||||
(C++11) | ||||
| Non-member functions | ||||
| Helper classes | ||||
(C++11) | ||||
unsignedlong to_ulong()const | (constexpr since C++23) | |
Converts the contents of the bitset to anunsignedlong 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.
Contents |
(none)
The converted integer.
Throwsstd::overflow_error if the value can not be represented inunsignedlong.
#include <bitset>#include <iostream>#include <stdexcept> int main(){for(unsignedlong i=0; i<10;++i){std::bitset<5> b(i);std::bitset<5> b_inverted= ~b;std::cout<< i<<'\t'<< b<<'\t'<< b_inverted<<'\t'<< b_inverted.to_ulong()<<'\n';} std::cout<<std::bitset<32>().to_string('-')<<'\n'; try{std::bitset<128> x(42);std::cout<< x.to_ulong()<<'\n'; x.flip();std::cout<< x.to_ulong()<<'\n';// throws}catch(conststd::overflow_error& ex){std::cout<<"ex: "<< ex.what()<<'\n';}}
Possible output:
0 00000 11111 311 00001 11110 302 00010 11101 293 00011 11100 284 00100 11011 275 00101 11010 266 00110 11001 257 00111 11000 248 01000 10111 239 01001 10110 22--------------------------------42ex: bitset to_ulong overflow error
| returns a string representation of the data (public member function)[edit] | |
(C++11) | returns anunsignedlonglong integer representation of the data (public member function)[edit] |