相关疑难解决方法(0)

带有bools的C++ bitfield打包

我刚刚用bitfields做了一个测试,结果令我感到惊讶.

class test1 {
public:
    bool test_a:1;
    bool test_b:1;
    bool test_c:1;
    bool test_d:1;
    bool test_e:1;
    bool test_f:1;
    bool test_g:1;
    bool test_h:1;
};

class test2 {
public:
    int test_a:1;
    int test_b:1;
    int test_c:1;
    int test_d:1;
    int test_e:1;
    int test_f:1;
    int test_g:1;
    int test_h:1;
};

class test3 {
public:
    int test_a:1;
    bool test_b:1;
    int test_c:1;
    bool test_d:1;
    int test_e:1;
    bool test_f:1;
    int test_g:1;
    bool test_h:1;
};
Run Code Online (Sandbox Code Playgroud)

结果如下: -

sizeof(test1) = 1   // This is what I'd expect. 8 bits in a byte …
Run Code Online (Sandbox Code Playgroud)

c++ bit-fields

30
推荐指数
4
解决办法
3万
查看次数

标签 统计

bit-fields ×1

c++ ×1