Code:
union fields_u {
unsigned short value;
// Anonymous structure...
struct {
unsigned short a:1;
unsigned short b:1;
unsigned short c:1;
unsigned short d:5;
unsigned short e:3;
unsigned short f:1;
unsigned short g:4;
};
};
// Usage:
union fields_u x;
x.value = 0x7bab;
// print the fields:
printf( "a:%u, b:%u, c:%u, d:0x%hx, e:0x%hx, f:%u, g:0x%hx\n",
x.a, x.b, x.c, x.d, x.e, x.f );
But, be careful... bit fields can be tricky. I advise to use the attribute 'packed' (or the pragma 'pack') to garantee the bits are tighter as possible...