c++ 位域(Bit Fields)
来源:互联网 发布:网络电视安装直播软件 编辑:程序博客网 时间:2024/05/01 20:32
C++ Bit Fields
Classes and structures can contain members that occupy less storage than an integral type. These members are specified as bit fields. The syntax for bit-field member-declarator specification follows:
declarator : constant-expression
The (optional) declarator is the name by which the member is accessed in the program. It must be an integral type (including enumerated types). The constant-expression specifies the number of bits the member occupies in the structure. Anonymous bit fields — that is, bit-field members with no identifier — can be used for padding.
An unnamed bit field of width 0 forces alignment of the next bit field to the next type boundary, where type is the type of the member.
The following example declares a structure that contains bit fields:
// bit_fields1.cpp// compile with: /LDstruct Date { unsigned short nWeekDay : 3; // 0..7 (3 bits) unsigned short nMonthDay : 6; // 0..31 (6 bits) unsigned short nMonth : 5; // 0..12 (5 bits) unsigned short nYear : 8; // 0..100 (8 bits)};
The conceptual memory layout of an object of type Date is shown in the following figure.
Note that nYear is 8 bits long and would overflow the word boundary of the declared type, unsigned short.Therefore, it is begun at the beginning of a new unsigned short.It is not necessary that all bit fields fit in one object of the underlying type; new units of storage are allocated, according to the number of bits requested in the declaration.
Microsoft Specific
The ordering of data declared as bit fields is from low to high bit, as shown in the figure above.
END Microsoft Specific
If the declaration of a structure includes an unnamed field of length 0, as shown in the following example,
// bit_fields2.cpp// compile with: /LDstruct Date { unsigned nWeekDay : 3; // 0..7 (3 bits) unsigned nMonthDay : 6; // 0..31 (6 bits) unsigned : 0; // Force alignment to next boundary. unsigned nMonth : 5; // 0..12 (5 bits) unsigned nYear : 8; // 0..100 (8 bits)};
the memory layout is as shown in the following figure.
The underlying type of a bit field must be an integral type, as described in Fundamental Types.
Reference
- C/C++ 位域(Bit Fields)
- C/C++位域(Bit-fields)
- C/C++位域(Bit-fields)
- C/C++位域(Bit-fields)
- 位域(bit fields)
- 关于位域bit fields
- c++ 位域(Bit Fields)
- c++ 位域(Bit Fields)
- 位域(Bit-fields)分析
- 位域(bit fields)简介
- 位域(bit fields)简介
- 位域(Bit-fields)分析
- 位域(bit fields)简介
- C/C++ 字节序/位域(Bit-fields)之我见
- 【转】 C/C++位域(Bit-fields)之我见
- C/C++位域(Bit-fields)之我见
- C/C++位域(Bit-fields)之我见
- C/C++位域(Bit-fields)之我见
- 基数排序 计数排序 桶排序
- 多媒体基础知识笔记
- Wine Trading in Gergovia
- 数据连接核心类(java层)
- 个人开公司流程
- c++ 位域(Bit Fields)
- 从svn log中提取版本号以及时间(从重复行中取出一行)
- android-详解Android中的屏幕方向
- Ajax 加载显示图标集合
- 市场营销学3——市场营销环境
- cdma移动台开机进入online的命令流程
- junit 总测
- git学习1
- linux工作模式下VI编辑