我如何在c ++中定义24位数组?

zoi*_*zoi 4 c++ bit-manipulation

我如何在c ++中定义24位数组?(变量声明)

str*_*ger 13

C++中没有24位变量类型.

您可以使用bitpacked结构:

struct ThreeBytes {
    uint32_t value:24;
};
Run Code Online (Sandbox Code Playgroud)

但不能保证sizeof ThreeBytes == 3.

您也可以根据需要使用uint32_tsint32_t.

另一个选择是使用std::bitset:

typedef std::bitset<24> ThreeBytes;
Run Code Online (Sandbox Code Playgroud)

然后制作一个数组:

ThreeBytes *myArray = new ThreeBytes[10];
Run Code Online (Sandbox Code Playgroud)

当然,如果你真的需要"三个字节",你可以创建一个数组数组:

typedef uint8_t ThreeBytes[3];
Run Code Online (Sandbox Code Playgroud)

请注意,uint8_t朋友是非标准的,仅用于澄清.