windows 位域的哪一端是最高有效位?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4547472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Which end of a bit field is the most significant bit?
提问by Jim Fell
I'm writing a C++ application for Windows XP/Vista/7 using Visual Studio 2008. Some of my structures use a bit field, as shown in the example.
我正在使用 Visual Studio 2008 为 Windows XP/Vista/7 编写 C++ 应用程序。我的一些结构使用位字段,如示例中所示。
typedef struct myStruct_tag
{
BYTE myVar1;
WORD myVar2;
WORD myVar3;
union
{
struct
{
BYTE :1;
BYTE field1 :1;
BYTE field2 :1;
BYTE reserved :5;
} myBitField;
BYTE myVar4;
};
BYTE myVar5;
BYTE myVar6;
} myStruct_t;
Which end of the field is the most significant bit?
字段的哪一端是最重要的位?
回答by Michael Burr
C99 standard 6.7.2.1/10 (emphasis mine):
C99 标准 6.7.2.1/10(强调我的):
An implementation may allocate any addressable storage unit large enough to hold a bitfield. If enough space remains, a bit-field that immediately follows another bit-field in a structure shall be packed into adjacent bits of the same unit. If insufficient space remains, whether a bit-field that does not fit is put into the next unit or overlaps adjacent units is implementation-defined. The order of allocation of bit-fields within a unit (high-order to low-order or low-order to high-order) is implementation-defined.The alignment of the addressable storage unit is unspecified.
一个实现可以分配任何足够大的可寻址存储单元来保存一个位域。如果剩余足够的空间,紧跟在结构中另一个位域之后的位域应被打包到同一单元的相邻位中。如果剩余空间不足,则不适合的位字段是否放入下一个单元或与相邻单元重叠是实现定义的。单元内位域的分配顺序(高阶到低阶或低阶到高阶)是实现定义的。可寻址存储单元的对齐方式未指定。
So, the order must be documented by your compiler implementation.
因此,您的编译器实现必须记录该顺序。
However, so much about how bitfields are implemented are implementation defined or unspecified that using them to model hardware, wire-protocol, or file format bit fields in a portable fashion is not worth the trouble to attempt.
然而,关于位域如何实现的很多是实现定义的或未指定的,因此使用它们以可移植的方式对硬件、连线协议或文件格式位域进行建模是不值得尝试的。
If you want your 'bit fields' to model something external to your program (like the above things), use explicit masks, setting and clearing the bits using the standard bit-wise operators (|
, '&,
~,
<<`, etc.). Use helper inline functions (or even macros if you must) to make this easier/clearer in your code.
如果您希望您的“位域”对程序外部的某些内容进行建模(如上述内容),请使用显式掩码,使用标准的按位运算符(|
, '& ,
~ ,
<<` 等)设置和清除位. 使用辅助内联函数(如果必须的话,甚至可以使用宏)使代码中的操作更容易/更清晰。
回答by rakslice
The Visual Studio 2008 compiler docs indicate:
Visual Studio 2008 编译器文档指出:
The ordering of data declared as bit fields is from low to high bit
声明为位域的数据的顺序是从低位到高位
From "C++ Bit Fields", MSDN C++ Language Reference, Visual Studio 2008 version
回答by Jay Maynard K5ZC
If you're asking about which bits in myBitField are stored in which bits of the byte in memory, that's explicitly undefined by the C standards. You'll have to learn by experimentation. It's probably worthwhile, if you're doing something where this actually matters, to instead use an approach where you #define field1
as a hex value (for example, 0x40
or 0x02
) and put it where you want it.
如果您询问 myBitField 中的哪些位存储在内存中字节的哪些位中,C 标准明确未定义。你必须通过实验来学习。如果您正在做一些真正重要的事情,那么使用将 #definefield1
作为十六进制值(例如,0x40
or 0x02
)并将其放在您想要的位置的方法,这可能是值得的。