Maximum size of a bit field in C or C++? [duplicate]

自作多情 提交于 2019-12-04 03:08:47

问题


Possible Duplicate:
struct bitfield max size (C99, C++)

Is there a limit to the number of bits that I can specify in a bit field in C or C++? For example, could I do this:

struct HugeInt {
    int myInt: 1000;
};

I'm asking about both C and C++, since I know that the language specs sometimes differ and wanted to see if the above example was guaranteed to work / not work in C or C++.


回答1:


In C11, section 6.7.2.1, clause 4:

The expression that specifies the width of a bit-field shall be an integer constant expression with a nonnegative value that does not exceed the width of an object of the type that would be specified were the colon and expression omitted. If the value is zero, the declaration shall have no declarator.

So in short, it must be between zero and the size of the type if it had not had a bit-field part.




回答2:


size of bit-field 'myInt' (1000 bits) cannot exceed size of its type (int, usually 32 bits)



来源:https://stackoverflow.com/questions/14553632/maximum-size-of-a-bit-field-in-c-or-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!