Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Bit-fields can be used to allow flags or other integer values with small ranges to be packed together to save storage space. When used in structure members, bit fields can improve storage efficiency. Compilers will typically allocate consecutive bit-field structure members to the same int-sized dword, as long as they fit into that completely into that dword. However, the order of allocation within a word is different in different implementations. This is known as the implementation's endianness. Big endian machines store the most significant bit at the lowest address versus little endian implementations, which store the most significant bit at the highest addressSome implementations are "right-to-left": the first member occupies the low-order position of the dword. Others are "left-to-right": the first member occupies the hhigh-order position of the dword. Depending of the order bits within a word dword can lead to different calculations on different implementations.

...

Code Block
struct  bf {
  unsigned m1 : 8;
  unsigned m2 : 8;
  unsigned m3 : 8;
  unsigned m4 : 8;

};;	/* 32 bits total */

/* ... */

Right-to-left Little endian implementations will allocate struct bf as:

Code Block
m4   m3   m2   m1

Conversely, big endian endian left-to-right implementations will allocate struct bf as:

...