Bitfield Ordering

The default bitfield ordering is big endian, meaning the first bits allocated take the highest bits as seen for a value held in a register. Pragma reverse_bitfields changes this allocation to the lower bits. When binary compatibility is required it is useful to specify an invariant declaration. Using an __attribute__ in the struct/class declaration serves that purpose.

Listing: Example - bitfield ordering
      typedef struct {
               unsigned long a:1;

               unsigned long b:3;

       } __attribute__((bitfields(little_endian)))   bf_le;

       typedef struct {

               unsigned long a:1;

               unsigned long b:3;

      } __attribute__((bitfields(big_endian)))   bf_be;

      bf_le le = { 1, 4 };

      bf_be be = { 1, 4 };

     struct __attribute__((bitfields(big_endian)))

     {

             unsigned long a:1;

             unsigned long b:3;

     } bebf = { 1, 4 };

     struct __attribute__((bitfields(little_endian)))

     {

             unsigned long a:1;

             unsigned long b:3;

     } lebf = { 1, 4 };