std::bitset::to_ulong(3)
π NAME π
std::bitset::to_ulong β literally std::bitset::to_ulong, bestie. That's the whole name, iconic and recursive-looking. This page is from the C++ Standard Libary manual, and we're about to make a bitset spill its integer tea. π
πΈ Synopsis πΈ
unsigned long to_ulong() const (constexpr since C++23)
okay so this member 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. periodt. π
π«Ά Parameters π«Ά
(none) β no parameters, no extra drama. π¦
β¨ Return value β¨
The converted integer. That's it. That's the return. π
π₯ Exceptions π₯
Throws std::overflow_error if the value can not be represented in unsigned long. (Yes, βcan notβ two words, we keep it.) Not me warning you: if it doesn't fit, it's throwing. π
π Example π
// Run this code
#include <bitset>
#include <iostream>
#include <stdexcept>
int main()
{
for (unsigned long 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 (const std::overflow_error& ex)
{
std::cout << "ex: " << ex.what() << '\n';
}
}
Possible output:
0 00000 11111 31
1 00001 11110 30
2 00010 11101 29
3 00011 11100 28
4 00100 11011 27
5 00101 11010 26
6 00110 11001 25
7 00111 11000 24
8 01000 10111 23
9 01001 10110 22
--------------------------------
42
ex: bitset to_ulong overflow error
π¦ See also π¦
to_stringβ returns a string representation of the data (public member function).to_ullongβ returns anunsigned long longinteger representation of the data (C++11) (public member function).
okay bestie, that's the whole vibe. This page is from http://cppreference.com, dated 2024.06.10, and the footer labels it std::bitset::to_ulong(3). slay, go convert responsibly. πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms