C 或 C++ 实现当然有可能提供这样大小的类型,但大多数实现不会提供具有这样大小的内置类型,因为 (1) 很少遇到它们,并且 (2) 大多数处理器不会遇到这种情况。不支持对此类类型的直接操作。
如果您尝试将这些整数用作二进制标志组,请考虑std::bitset按照其他答案的建议使用。这甚至可能是一个更好的选择,因为它明确表明您正在使用一组标志。例如:
std::bitset<6> bits; // Six independent bit flags
bits[3] = true; // Set the third flag
Run Code Online (Sandbox Code Playgroud)
如果您尝试将它们用作实际的整数类型,但仅受它们使用的位数限制,请考虑使用位域,如下所示:
struct uint6_t {
uint64_t value : 6; // 6 bits, unsigned
};
struct int6_t {
int64_t value : 6; // 6 bits, signed
};
Run Code Online (Sandbox Code Playgroud)
然后您可以将uint6_t的value字段用作六位整数。这仅适用于小于位域内使用的基础类型大小的大小,该大小应适用于 6 或 11 等大小,但不适用于 137 或 271 等大小。请注意 - 这些对象的实际大小将由于编译器引入的填充位,它们可能不是六位,但它们的功能仍然像六位整数一样。
显然 C++ 模板允许你做这样的事情:
template <unsigned int NumBits> struct uint {
uint64_t data : NumBits;
};
template <unsigned int NumBits> struct Int {
int64_t data : NumBits;
};
uint<6> value;
value.data = 0; // Or other uses
Int<6> value;
value.data = -1; // Or other uses
Run Code Online (Sandbox Code Playgroud)
编辑:根据您想要做的事情,您似乎正在寻找这样的东西:
uint<6> value;
value.data = -3;
std::cout << value.data << std::endl; // Prints 3
Run Code Online (Sandbox Code Playgroud)