Alignment of bit fields

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. The alignment of a bit field depends on its base type and the compilation mode (32-bit or 64-bit).

Note: long long and unsigned long long are not available for C on AIX®.

C 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.

C++ The length of a bit field can exceed the length of its base type, but the remaining bits are used to pad the field, and do not actually store any value.

However, alignment rules for aggregates containing bit fields are different depending on the alignment mode in effect. These rules are described below.

Rules for natural alignment

Rules for power alignment

Rules for Mac68K alignment

Rules for bit-packed alignment

Example of bit-packed alignment

#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