C bit structure

Byron Clark byron at theclarkfamily.name
Thu Mar 19 19:12:01 MDT 2009

On Thu, Mar 19, 2009 at 06:33:48PM -0600, Alec Shaw wrote:
> I want to implement a struct that is 4 bytes in size that has alternate  
> bit fields
> similar to the following.  The struct below is 12 bytes in size.
> Any suggestions on how create a similar structure of only 4 bytes.
> I want to overlay the structure on a character stream.

Note that this is inherently compiler dependent.  That said, here's how
you could define that struct for gcc.

#include <stdio.h>
#include <stdint.h>

struct __attribute__ ((__packed__)) mystruct
   uint8_t a;                                                                                                                       
   union {                                                                                                                                 
       struct {                                                                                                                            
           uint8_t one_ba:4;                                                                                                             
           uint8_t one_bb:4;                                                                                                             
       } type1b;                                                                                                                           
       struct {                                                                                                                            
           uint8_t two_ba:8;                                                                                                             
       } type2b;                                                                                                                           
       struct {                                                                                                                            
           uint8_t three_ba:1;                                                                                                             
           uint8_t three_bb:1;                                                                                                             
           uint8_t three_bc:1;                                                                                                             
           uint8_t three_bd:2;                                                                                                             
           uint8_t three_be:3;                                                                                                             
       } type3b;                                                                                                                           
   } u;                                                                                                                                    
   uint16_t c;                                                                                                                      

int main(int argc, char* argv[])
    printf("sizeof(struct mystruct) => %lu\n", sizeof(struct mystruct));
    return 0;

Byron Clark

More information about the PLUG mailing list