struct bitfield max size (C99, C++)

后端 未结 4 1243
不知归路
不知归路 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:48

    Since the values of bit-fields are assigned to integers, I'd assume that the largest bit-field value you can use is that of the size of intmax_t.

    Edit:

    From the C99 Spec:

    6.7.2.1 Bullet 9:

    A bit-field is interpreted as a signed or unsigned integer type consisting of the specified number of bits. If the value 0 or 1 is stored into a nonzero-width bit-field of type _Bool, the value of the bit-field shall compare equal to the value stored.

    6.7.2.1 Bullet 10:

    An implementation may allocate any addressable storage unit large enough to hold a bit- field. 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.

提交回复
热议问题