30 August 2010

Using struct to represent bit fields

#include <stdio.h>

struct packed_data
{
unsigned int :3;
unsigned int f1:1;
unsigned int f2:1;
unsigned int f3:1;
unsigned int type:8;
unsigned int index:18;
};
int main(void)
{

struct packed_data pd;
pd.type = 257;
printf("%u\n", pd.type);

pd.f1 = 4;
printf("%u\n", pd.f1);

//printf("%p\n", &(pd.f1)); ERROR

return 0;
}

Post a Comment