struct bitfield max size (C99, C++)

后端 未结 4 1240
不知归路
不知归路 2020-12-06 01:47

What is maximal bit width for bit struct field?

struct i { long long i:127;}

Can I define a bit field inside struct, with size of bitfield

4条回答
  •  情深已故
    2020-12-06 01:57

    C99 §6.7.2.1, paragraph 3:

    The expression that specifies the width of a bit-field shall be an integer constant expression that has nonnegative value that shall not exceed the number of bits in an object of the type that is specified if the colon and expression are omitted. If the value is zero, the declaration shall have no declarator.

    C++0xa §9.6, paragraph 1:

    ... The constant-expression shall be an integral constant expression with a value greater than or equal to zero. The value of the integral constant expression may be larger than the number of bits in the object representation (3.9) of the bit-field’s type; in such cases the extra bits are used as padding bits and do not participate in the value representation (3.9) of the bit-field.

    So in C you can't do that at all, and in C++ it won't do what you want it to.

提交回复
热议问题