/* --------------------------------------------------------------------- ** Figure 18.18: Bitfield-structure demonstration. ** --------------------------------------------------------------------- */ #include typedef struct DEMO{ unsigned int one : 1 ; unsigned int two : 3 ; unsigned int three :10 ; unsigned int four : 5 ; unsigned int : 2 ; unsigned int five : 8 ; unsigned int six : 8 ; } demo_type; void main( void ) { int k; unsigned char* bptr; demo_type bit = { 1, 5, 513, 17, 129, 0x81 }; printf( "\n sizeof demo_type = %lu\n", sizeof(demo_type) ); printf( " initial values: bit = %u, %u, %u, %u, %u, %u\n", bit.one, bit.two, bit.three, bit.four, bit.five, bit.six ); bptr = (unsigned char*)&bit; printf( " hex dump of bit: %02x %02x %02x %02x %02x %02x\n", bptr[0], bptr[1], bptr[2], bptr[3], bptr[4], bptr[5] ); bit.three = 1023; printf( "\n assign 1023 to bit.three: %u, %u, %u, %u, %u, %u\n", bit.one, bit.two, bit.three, bit.four, bit.five, bit.six ); k = bit.two; printf( " assign bit.two to k: k = %u\n", k ); }