You can declare a bit field as a _Bool (C), bool (C++), char, signed char, unsigned char, short, unsigned short, int, unsigned int, long, unsigned long, long long, or unsigned long long data type. A bit field is always 4 or 8 bytes, depending on the declared base type and the compilation mode (32-bit or 64-bit).
In the C language, you can specify bit fields as char or
short instead of int, but XL C/C++ maps them as if they were
unsigned int. The length of a bit field cannot exceed the
length of its base type. In extended mode, you can use the
sizeof operator on a bit field. (The sizeof operator
on a bit field always returns 4.)
The length of a bit field can exceed the length of its base type, but the
remaining bits will be used to pad the field, and will not actually store any
value.
However, alignment rules for aggregates containing bit fields are different depending on the alignment setting you specify. These rules are described below.
For:
#pragma options align=bit_packed struct { int a : 8; int b : 10; int c : 12; int d : 4; int e : 3; int : 0; int f : 1; char g; } A; pragma options align=reset
The size of A is 7 bytes. The alignment of
A is 1 byte. The layout of A is:
Member name | Byte offset | Bit offset |
---|---|---|
a | 0 | 0 |
b | 1 | 0 |
c | 2 | 2 |
d | 3 | 6 |
e | 4 | 2 |
f | 5 | 0 |
g | 6 | 0 |