C 中变量名后面的冒号
Possible Duplicate:
What does a colon in a struct declaration mean, such as :1, :7, :16, or :32?
This is C code sample of a reference page.
signed int _exponent:8;
What's the meaning of the colon before '8' and '8' itself?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个位域。它仅在
struct
定义中有效,这意味着系统将仅对您的整数使用 8 位。It's a bitfield. It's only valid in a
struct
definition, and it means that the system will only use 8 bits for your integer.它是一个位域,是结构的一个模糊且误导性的特征。这应该足以让您查找处理其他人代码中的位字段所需的信息。至于您自己的代码,切勿使用位域。
编辑:根据 Zack 的要求,与执行您自己的位算术相比,位域具有显着的缺点,并且没有优点。以下是其中一些:
对于单位标志,使用您自己的位算术而不是位域是完全无需考虑的。对于需要打包的较大值,如果在各处写出所有位算术太痛苦,请编写一些简单的宏。
It's a bitfield, an obscure and misguided feature of structures. That should be enough for you to lookup the information you need to know to deal with bitfields in other people's code. As for your own code, never use bitfields.
Edit: As requesed by Zack, bitfields have significant disadvantages versus performing your own bit arithmetic, and no advantages. Here are some of them:
For single-bit flags, using your own bit arithmetic instead of bitfields is a complete no-brainer. For larger values you need to pack, if it's too painful to write out all the bit arithmetic all over the place, write some simple macros.
它是一个位字段规范。
这意味着
_exponent
仅占用signed int
中的8
位,而通常需要超过8
位。通常,位字段与无符号类型一起使用。IIRC,如果将不适合 8 位的内容写入
_exponent
,编译器会发出警告。It is a bitfield specification.
It means
_exponent
takes only8
bits out of thesigned int
which typically takes more than8
bits. Typically, bit-fields are used with unsigned types.IIRC, compiler would warn if a something that does not fit into 8-bits is written into
_exponent
.当该语句位于结构内部时,意味着 位字段。
When that statement is inside a structure, means bit fields.