C ++中的10或12位字段数据类型 [英] 10 or 12 bit field data type in C++
本文介绍了C ++中的10或12位字段数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用type-def定义一些奇数大小的数据类型而不是标准类型,例如C ++中的10位或12位?
Is it possible to define some odd sized data type instead of the standard types using type-def like 10 bit or 12 bit in C++ ?
推荐答案
您可以为此使用位域:
struct bit_field
{
unsigned x: 10; // 10 bits
};
并像这样使用它
bit_field b;
b.x = 15;
示例:
#include <iostream>
struct bit_field
{
unsigned x: 10; // 10 bits
};
int main()
{
bit_field b;
b.x = 1023;
std::cout << b.x << std::endl;
b.x = 1024; // now we overflow our 10 bits
std::cout << b.x << std::endl;
}
AFAIK,在<$ c $之外没有位域的方法c> struct ,即
unsigned x: 10;
本身无效。
这篇关于C ++中的10或12位字段数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文