Keil Logo

C51: Bit Field Unions Give Strange Results


Information in this article applies to:

  • C51 All Versions

SYMPTOM

When using a bit field structure in a union, values stored in the bit fields do not appear to be stored in the right place. For example:

struct byte_nibbles
  {
  unsigned char b1: 4;
  unsigned char b2: 4;
  unsigned char b3: 4;
  unsigned char b4: 4;
  unsigned char b5: 4;
  unsigned char b6: 4;
  unsigned char b7: 4;
  unsigned char b8: 4;
  };

union
  {
  unsigned long var;
  struct byte_nibbles b;
  }
u;

void main (void)
{
u.b.b1=0x01; u.b.b2=0x02; u.b.b3=0x03; u.b.b4=0x04;
u.b.b5=0x05; u.b.b6=0x06; u.b.b7=0x07; u.b.b8=0x08;
}

stores the value 0x21436587 in u.var.

CAUSE

This is caused by the way the compiler stores bit fields. Bit fields are stored starting with the LSB. In this case, bit fields are stored in bytes (because the field type is unsigned char). So, the first byte in the union is filled with 1 (in the Least Significant 4 bits) and 2 (in the Most Significant 4 bits). The second byte in the union is filled with 3 (in the Least Significant 4 bits) and 4 (in the Most Significant 4 bits). And so on.

RESOLUTION

To store the nibbles in a different order, you must simply change the names of the bit fields. For example, changing the byte_nibbles structure definition to the following:

struct byte_nibbles
  {
  unsigned char b2: 4;
  unsigned char b1: 4;
  unsigned char b4: 4;
  unsigned char b3: 4;
  unsigned char b6: 4;
  unsigned char b5: 4;
  unsigned char b8: 4;
  unsigned char b7: 4;
  };

causes the above example to store the value 0x12345678 in u.var.

Changing the byte_nibbles structure definition to the following:

struct byte_nibbles
  {
  unsigned char b7: 4;
  unsigned char b8: 4;
  unsigned char b5: 4;
  unsigned char b6: 4;
  unsigned char b3: 4;
  unsigned char b4: 4;
  unsigned char b1: 4;
  unsigned char b2: 4;
  };

causes the above example to store the value 0x87654321 in u.var.

SEE ALSO


Last Reviewed: Thursday, February 25, 2021


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.