I have a structure that is intented to use 32 bits of storage:
struct foo_t {
color_t color : 10
some_type_t some_field : 22;
}
, where color_t
is an enum defined as
typedef enum {
RED = 0,
// other values...
BLUE = 255
} color_t
Note that color_t
values currently fit in 8 bits, although in the future we might add more values (thus we reserved 10 bits for color
)
In C99, I wonder if there is any guarantee that the width of color
will be respected by the compiler. As discussed in this question, the compiler might choose to represent color_t
as a char. At that point, the specified width appears incorrect according to the C99 spec:
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.
How can I enforce that the color
field uses 10 bits, then? Note that the problem goes away if the compiler used a regular integer to represent color_t
, but this behavior cannot be assumed.