Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:24

0001 // name=unionTest ; gcc $name.cc -std=c++11 -lstdc++ -o /tmp/$name && /tmp/$name
0002 
0003 /**
0004 
0005 https://stackoverflow.com/questions/252552/why-do-we-need-c-unions
0006 
0007 **/
0008 
0009 
0010 typedef union
0011 {
0012     struct {
0013         unsigned char byte0 ;
0014         unsigned char byte1 ;
0015         unsigned char byte2 ;
0016         unsigned char byte3 ;
0017     } bytes;
0018     unsigned int dword;
0019 } HW_Register;
0020 
0021 
0022 union HW_Register2
0023 {
0024     struct {
0025         unsigned char byte0 ;
0026         unsigned char byte1 ;
0027         unsigned char byte2 ;
0028         unsigned char byte3 ;
0029     } bytes ;
0030     unsigned int dword;
0031 }; 
0032 
0033 
0034 
0035 
0036 
0037 #include <cstdio>
0038 
0039 int main()
0040 {
0041     //HW_Register reg;
0042     HW_Register2 reg;
0043 
0044     reg.dword = 0x12345678;
0045 
0046     printf("// reg.dword        %x \n" , reg.dword ); 
0047     printf("// reg.bytes.byte0  %x \n" , reg.bytes.byte0 ); 
0048     printf("// reg.bytes.byte1  %x \n" , reg.bytes.byte1 ); 
0049     printf("// reg.bytes.byte2  %x \n" , reg.bytes.byte2 ); 
0050     printf("// reg.bytes.byte3  %x \n" , reg.bytes.byte3 ); 
0051 
0052 
0053 
0054 
0055     return 0 ; 
0056 }
0057