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

CThe 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 the size of the base type.

C++ 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 mode in effect. These rules are described below.

Rules for Linux® PowerPC® alignment

Rules for bit-packed alignment

Example of bit-packed alignment

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