命名空间
变体
操作

std::bitset<N>::to_ulong

来自 cppreference.cn
< cpp‎ | utility‎ | bitset
 
 
 
 
unsigned long to_ulong() const
(constexpr since C++23)

将 bitset 的内容转换为 unsigned long 整数。

bitset 的第一位对应于数字的最低有效位,最后一位对应于最高有效位。

目录

[编辑] 参数

(无)

[编辑] 返回值

转换后的整数。

[编辑] 异常

如果该值无法用 unsigned long 表示,则抛出 std::overflow_error

[编辑] 示例

#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';
    }
}

可能的输出

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

[编辑] 参见

返回数据的字符串表示形式
(public member function) [编辑]
(C++11)
返回数据的 unsigned long long 整数表示形式
(public member function) [编辑]