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;
}

1 comment:

John said...

Wow thats awesome i must say its a cool blog.

http://buy-tools.blogspot.com/