蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

实测 std::bitset 初始化、逐位修改、LSB 位序遍历、按位逻辑运算与 128 位溢出异常

CPP本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。explore-cpp-libs-bitset.cpp
explore-cpp-libs-bitset.cpp136 行
#include <bitset>
#include <cstdio>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>

namespace lab {

struct Case {
    std::string name;
    std::string expected_json;
    std::string actual_json;
    bool passed;
};

std::string esc(const std::string& s) {
    std::string out = "\"";
    for (unsigned char c : s) {
        switch (c) {
            case '\"': out += "\\\""; break;
            case '\\': out += "\\\\"; break;
            case '\b': out += "\\b"; break;
            case '\f': out += "\\f"; break;
            case '\n': out += "\\n"; break;
            case '\r': out += "\\r"; break;
            case '\t': out += "\\t"; break;
            default:
                if (c < 0x20) {
                    char buf[8];
                    std::snprintf(buf, sizeof(buf), "\\u%04x", c);
                    out += buf;
                } else {
                    out += static_cast<char>(c);
                }
        }
    }
    out += "\"";
    return out;
}

std::string jbool(bool v) { return v ? "true" : "false"; }

void emit(const std::vector<Case>& cases) {
    std::cout << "{\"runtime\": \"G++ C++17 (mstorsjo/llvm-mingw:20260616)\", \"cases\": [";
    for (std::size_t i = 0; i < cases.size(); ++i) {
        if (i) std::cout << ", ";
        std::cout << "{\"name\": " << esc(cases[i].name)
                  << ", \"expected\": " << cases[i].expected_json
                  << ", \"actual\": " << cases[i].actual_json
                  << ", \"passed\": " << jbool(cases[i].passed) << "}";
    }
    std::cout << "]}\n";
}

} // namespace lab

int main() {
    std::vector<lab::Case> cases;

    // Case 1: initialization (default, integer, string) & operator[]/set/reset/flip
    std::bitset<8> b_def;
    std::bitset<8> b_int(42);
    std::bitset<8> b_mod("00001111");
    b_mod[0] = 1;
    b_mod.set(4);
    b_mod.reset(1);
    b_mod.flip();
    std::string act1 = b_def.to_string() + "|" + b_int.to_string() + "|" + b_mod.to_string();
    std::string exp1 = "00000000|00101010|11100010";
    cases.push_back({
        "bitset_init_and_modify",
        lab::esc(exp1),
        lab::esc(act1),
        act1 == exp1
    });

    // Case 2: query functions and LSB-first index order
    std::bitset<8> bq("10101010");
    std::bitset<8> b_order("11001010");
    std::string lsb_loop;
    for (std::size_t i = 0; i < bq.size(); ++i) {
        lsb_loop += bq[i] ? '1' : '0';
    }
    std::string act2 = "cnt=" + std::to_string(bq.count())
        + ";sz=" + std::to_string(bq.size())
        + ";t3=" + std::to_string(bq.test(3) ? 1 : 0)
        + ";all=" + std::to_string(bq.all() ? 1 : 0)
        + ";any=" + std::to_string(bq.any() ? 1 : 0)
        + ";none=" + std::to_string(bq.none() ? 1 : 0)
        + ";order3=" + std::to_string(b_order[3] ? 1 : 0)
        + ";lsb=" + lsb_loop;
    std::string exp2 = "cnt=4;sz=8;t3=1;all=0;any=1;none=0;order3=1;lsb=01010101";
    cases.push_back({
        "bitset_query_and_bit_order",
        lab::esc(exp2),
        lab::esc(act2),
        act2 == exp2
    });

    // Case 3: bitwise &, |, ^, ~
    std::bitset<8> b1("10101010");
    std::bitset<8> b2("11110000");
    std::string act3 = (b1 & b2).to_string() + "|" + (b1 | b2).to_string() + "|" + (b1 ^ b2).to_string() + "|" + (~b1).to_string();
    std::string exp3 = "10100000|11111010|01011010|01010101";
    cases.push_back({
        "bitset_bitwise_ops",
        lab::esc(exp3),
        lab::esc(act3),
        act3 == exp3
    });

    // Case 4: to_ulong, to_ullong, to_string and overflow_error for >64-bit value
    unsigned long ul = b1.to_ulong();
    unsigned long long ull = b1.to_ullong();
    std::bitset<128> big;
    big.set(100);
    bool threw_ovf = false;
    try {
        (void)big.to_ullong();
    } catch (const std::overflow_error&) {
        threw_ovf = true;
    }
    std::string act4 = "ul=" + std::to_string(ul) + ";ull=" + std::to_string(ull) + ";str=" + b1.to_string() + ";ovf=" + std::to_string(threw_ovf ? 1 : 0);
    std::string exp4 = "ul=170;ull=170;str=10101010;ovf=1";
    cases.push_back({
        "bitset_conversions_and_overflow",
        lab::esc(exp4),
        lab::esc(act4),
        act4 == exp4
    });

    lab::emit(cases);
    return 0;
}
本例需在 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。

入口文件:projects/cplusplus-cpp-libs-bitset-lab/explore-cpp-libs-bitset.cpp

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-cpp-libs-bitset.cpp,或下载右侧课程工程包。
  2. 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:C++

课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip

下载课程关联工程包:C++

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2332.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习