//------------------------------------------------------------------- // bitfield.c // // This program investigates the GNU compiler's arrangement of // bitfields within a 'struct', something outside the language // standards for the C programming language, but important for // properly accessing several hardware-defined field-elements. // // compile using: $ gcc bitfield.c -o bitfield // execute using: $ ./bitfield // // programmer: ALLAN CRUSE // written on: 07 APR 2008 //------------------------------------------------------------------- #include // for printf() typedef struct { unsigned char hlen:4; // bits 3..0 unsigned char vers:4; // bits 7..4 } IP_BYTE; typedef struct { unsigned int desc_status:20; // bits 0..19 unsigned int desc_errors:12; // bits 20..31 } TXD_EXT; int main( int argc, char **argv ) { IP_BYTE ipb; TXD_EXT txd; ipb.vers = 4; ipb.hlen = 5; txd.desc_status = 0x00001; // DD-bit set txd.desc_errors = 0xFFF; // all bits set printf( "IP_BYTE: 0x%02X \n", *(unsigned char *)&ipb ); printf( "TXD_EXT: 0x%08X \n", *(unsigned int *)&txd ); }