Datasheet
ARM Compiler Reference
ARM DUI 0067D Copyright © 1999-2001 ARM Limited. All rights reserved. 3-29
struct X {
int x:10;
int y:20;
int z:5;
};
The compiler pads the remaining two bits for the first container and assigns a new
integer container for
z
.
Bitfield containers can overlap each other, for example:
struct X {
int x:10;
char y:2;
};
The first declaration creates an integer container and allocates 10 bits to
x
. These 10 bits
occupy the first byte and two bits of the second byte of the integer container. At the
second declaration, the compiler checks for a container of type
char
. There is no suitable
container, so the compiler allocates a new correctly aligned
char
container.
Because the natural alignment of
char
is 1, the compiler searches for the first byte that
contains a sufficient number of unallocated bits to completely contain the bitfield. In the
above example, the second byte of the
int
container has two bits allocated to
x
, and six
bits unallocated. The compiler allocates a
char
container starting at the second byte of
the previous
int
container, skips the first two bits that are allocated to
x
, and allocates
two bits to
y
.
If
y
is declared
char y:8
, the compiler pads the second byte and allocates a new
char
container to the third byte, because the bitfield cannot overflow its container
(Figure 3-2).
struct X {
int x:10;
char y:8;
};
Figure 3-2 Bitfield allocation 1
Note
The same basic rules apply to bitfield declarations with different container types. For
example, adding an
int
bitfield to the example above gives:
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
xyunallocated padding
Bit number