std::bitset::to_ullong(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… std::bitset::to_ullong β€” the NAME section, bestie

ok so this page is std::bitset::to_ullong from the C++ Standard Library, and honestly? it's giving "glow-up for your bits." πŸ¦‹


✨ Synopsis ✨

unsigned long long to_ullong() const  (since C++11)
                                      (constexpr since C++23)

Converts the contents of the bitset to an unsigned long long integer. Periodt. πŸ”₯

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. So like… read it backwards in your head, it's fine, we've all been there. 🫢


πŸŽ€ Parameters πŸŽ€

(none)

Nothing. Zero. Nada. Not a single argument, she takes no notes. πŸ’…


⭐ Return value ⭐

The converted integer. That's it, that's the whole vibe. ✨


😭 Exceptions 😭

  • std::overflow_error if the value can not be represented in unsigned long long.
fr fr, if your bitset is too big for an unsigned long long to hold, she's gonna throw. No cap. Don't say I didn't warn you. πŸ’€

🌸 Example 🌸

// Run this code

#include <bitset>
#include <iostream>
#include <limits>

int main()
{
    std::bitset<std::numeric_limits<unsigned long long>::digits> b
    (
        0x123456789abcdef0LL
    );

    std::cout << b << "  " << std::hex << b.to_ullong() << '\n';
    b.flip();
    std::cout << b << "  " << b.to_ullong() << '\n';

    std::bitset<std::numeric_limits<unsigned long long>::digits + 1> q{0};
    try
    {
        (~q).to_ullong(); // throws
    }
    catch (const std::overflow_error& ex)
    {
        std::cout << "ex: " << ex.what() << '\n';
    }
}

πŸ’– Output πŸ’–

0001001000110100010101100111100010011010101111001101111011110000
123456789abcdef0
1110110111001011101010011000011101100101010000110010000100001111
edcba9876543210f
ex: _Base_bitset::_M_do_to_ullong

like… look at her go. b.flip() said "new bits, who dis" and the whole number changed. iconic behavior honestly. πŸ‘‘


🌈 See also 🌈

  • to_string β€” returns a string representation of the data (public member function)
  • to_ulong β€” returns an unsigned long integer representation of the data (public member function)

ok that's the whole page bestie, go forth and convert your bits responsibly πŸ’œ

source/version + date drop, since you gotta cite your sources even when you're cute: straight from http://cppreference.com, the 2024.06.10 edition. mwah. πŸ“πŸ’–
πŸ“¦ where this page comes from