哪一个位字段是最重要的一点?

哪一个位字段是最重要的一点?,第1张

概述我正在使用Visual Studio 2008为 Windows XP / Vista / 7编写C应用程序.我的一些结构使用一个位字段,如示例所示. typedef struct myStruct_tag{ BYTE myVar1; WORD myVar2; WORD myVar3; union { struct { 我正在使用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;

该领域的哪一端是最重要的一点?

解决方法 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.

因此,您的编译器实现必须记录顺序.

然而,关于如何实现位域是实现定义或未指定的,使用它们以便携式方式对硬件,有线协议或文件格式位字段进行建模是不值得尝试的麻烦.

如果你想让你的“位字段”对程序外部的东西进行建模(像上面的那样),使用显式掩码,使用标准的位运算符(|,’&,〜,& ;`等等).使用帮助内联函数(如果必须的话,甚至是宏),使您的代码更容易/更清晰.

总结

以上是内存溢出为你收集整理的哪一个位字段是最重要的一点?全部内容,希望文章能够帮你解决哪一个位字段是最重要的一点?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1233430.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存