Search code examples
cstructendiannessbit-fields

How endianness effects on bitfields when a bitfield is greater than size of byte


I have below program.

#include<stdio.h>                                                                                                                                                                                    
#include<string.h>                                                                                                                                                                                   

struct abc {                                                                                                                                                                                         
  int i;                                                                                                                                                                                             
  char ar[10];                                                                                                                                                                                       
  int j:9;                                                                                                                                                                                           
  int k:3;                                                                                                                                                                                           
}abc1;                                                                                                                                                                                               


void wasim_dump_data(const void *object, size_t size)
{                                                    
  int i;                                             
  printf("[ \n");                                    
  for(i = 0; i < size; i++)                          
  {                                                  
    if (i%4 ==0)                                     
      printf("[ ");                                  
    printf("%02x ", ((const unsigned char *) object)[i] & 0xff);
    if ((i+1)%4 == 0)                                           
      printf("]\n");                                            
  }                                                             
  printf("]\n");                                                

}

int main ()
{          
  strcpy (abc1.ar, "wasim");
  abc1.i=5;                 
  abc1.j=2;                 
  abc1.k=3;                 

  struct abc * p1 = &abc1;
  printf("abc1.ar : %s\nabc1.i : %d\nabc1.j : %d\nabc1.k : %d\n",abc1.ar, abc1.i, abc1.j, abc1.k);
  wasim_dump_data (p1,sizeof(abc1));                                                              

  return 0;
}

which gives the below output

abc1.ar : wasim
abc1.i : 5
abc1.j : 2
abc1.k : 3
[
[ 05 00 00 00 ]
[ 77 61 73 69 ]
[ 6d 00 00 00 ]
[ 00 00 02 06 ]
]

How come 2 and 3 stored in memory as 02 and 06. How odd sized bitfields which size is more than a byte stored in memory?


Solution

  • The compiler packs the bitfields as follows in the last two bytes:

      JJJJJJJJ00000KKKJ
      00000010000000110 = 02 06
    

    where the final J is the most significant bit of j. Note that almost everything about bitfields is implementation-defined or unspecified so you cannot rely on this. If you need total control of the layout, don't use bitfields but unsigned types and bit shifting plus masking.