|
|
|
struct Disk_Register
{
unsigned int ready:1 ; // 1 bit field named "ready"
unsigned int error:1 ; // 1 bit field named "error"
unsigned int wr_prot:1 ;
unsigned int dsk_spinning:1 ;
unsigned int command:4 ; // 4 bits field named "command"
unsigned int error_code:8 ;
unsigned int sector_no:16 ;
};
int main( int argc, char* argv[] )
{
struct Disk_Register r;
printf( "sizeof(r) = %d\n", sizeof(r) ); // 4 bytes (32 bits)
int* p = (int *) &r; // Access r as in int through pointer p
*p = 0; // Clear all 32 bits in r !
r.error = 1; // Set the error bit (bit #30)
printBits( *p ); // Call the printBits() function
putchar('\n');
r.dsk_spinning = 1; // Set the dsk_spinning bit (bit #28)
printBits( *p ); // Call the printBits() function
putchar('\n');
}
|
Output:
sizeof(r) = 4 01000000000000000000000000000000 01010000000000000000000000000000 |
How to run the program:
|
|
/* -------------------------------------------------------
Define the mapping of the 32 bits in the Disk Register
------------------------------------------------------- */
struct Disk_Register
{
unsigned int ready:1 ; // 1 bit field named "ready"
unsigned int error:1 ; // 1 bit field named "error"
unsigned int wr_prot:1 ;
unsigned int dsk_spinning:1 ;
unsigned int command:4 ; // 4 bits field named "command"
unsigned int error_code:8 ;
unsigned int sector_no:16 ;
};
/* ==========================================================
Re-map the 32 bits Disk Register AND a integer together
========================================================== */
union U_Disk_Register
{
struct Disk_Register Reg; // (1) 32 bits mapped as struct Disk_Register
int Whole_Reg; // (2) 32 bits as one int
};
|
Notice that:
|
Whole_Reg
|
Example:
struct Disk_Register
{
unsigned int ready:1 ; // 1 bit field named "ready"
unsigned int error:1 ; // 1 bit field named "error"
unsigned int wr_prot:1 ;
unsigned int dsk_spinning:1 ;
unsigned int command:4 ; // 4 bits field named "command"
unsigned int error_code:8 ;
unsigned int sector_no:16 ;
};
/* ==========================================================
Re-map the 32 bits Disk Register AND a integer together
========================================================== */
union U_Disk_Register
{
struct Disk_Register Reg; // (1) 32 bits mapped as struct Disk_Register
int Whole_Reg; // (2) 32 bits as one int
};
int main( int argc, char* argv[] )
{
union U_Disk_Register r;
printf( "sizeof(r) = %d\n", sizeof(r) ); // Still 4 bytes !!!
r.Whole_Reg = 0; // Clear all 32 bits
r.Reg.error = 1; // Set the error bit (bit #30)
printBits( r.Whole_Reg ); // Call the printBits() function
putchar('\n');
r.Reg.dsk_spinning = 1; // Set the dsk_spinning bit (bit #28)
printBits( r.Whole_Reg ); // Call the printBits() function
putchar('\n');
}
|
Output:
sizeof(r) = 4 01000000000000000000000000000000 01010000000000000000000000000000 |
How to run the program:
|
|
|
|
struct MyMap
{
unsigned int b31:1;
unsigned int b30:1;
unsigned int b29:1;
unsigned int b28:1;
unsigned int b27:1;
unsigned int b26:1;
unsigned int b25:1;
unsigned int b24:1;
unsigned int b23:1;
unsigned int b22:1;
unsigned int b21:1;
unsigned int b20:1;
unsigned int b19:1;
unsigned int b18:1;
unsigned int b17:1;
unsigned int b16:1;
unsigned int b15:1;
unsigned int b14:1;
unsigned int b13:1;
unsigned int b12:1;
unsigned int b11:1;
unsigned int b10:1;
unsigned int b9:1;
unsigned int b8:1;
unsigned int b7:1;
unsigned int b6:1;
unsigned int b5:1;
unsigned int b4:1;
unsigned int b3:1;
unsigned int b2:1;
unsigned int b1:1;
unsigned int b0:1;
};
int main( int argc, char* argv[] )
{
struct MyMap r;
printf( "sizeof(r) = %lu\n", sizeof(r) );
int *p = (int *) &r; // Make p point to r
*p = 0; // Clear all 32 bits in r
printBits( *p ); // Print the bits in r
putchar('\n');
r.b0 = 1;
printBits( *p ); // Print the bits in r
putchar('\n');
r.b8 = 1;
r.b9 = 1;
printBits( *p ); // Print the bits in r
putchar('\n');
r.b16 = 1;
r.b17 = 1;
r.b18 = 1;
printBits( *p ); // Print the bits in r
putchar('\n');
r.b30 = 1;
printBits( *p ); // Print the bits in r
putchar('\n');
}
|
When run on solar:
sizeof(r) = 4 00000000000000000000000000000000 (left to right order) 00000000000000000000000000000001 00000000000000000000001100000001 00000000000001110000001100000001 01000000000001110000001100000001 |
When run on a lab machine (linux):
sizeof(r) = 4 00000000000000000000000000000000 (right to left order !) 10000000000000000000000000000000 10000000110000000000000000000000 10000000110000001110000000000000 10000000110000001110000000000010 |
How to run the program:
|