home *** CD-ROM | disk | FTP | other *** search
/ C by Discovery (4th Edition) / C_By_Discovery_4th_Edition.tar / C_By_Discovery_4th_Edition / _DISK_ / ch7 / union2.c < prev    next >
C/C++ Source or Header  |  2005-06-16  |  3KB  |  64 lines

  1. /*               union2.c
  2.  *
  3.  *   Synopsis  - Displays the values of the bits and the bytes 
  4.  *               in a short int.
  5.  *
  6.  *   Objective - To illustrate the use of bit fields and unions
  7.  *               in accessing the bits and bytes of a quantity.
  8.  */
  9.  
  10. /* Include Files */
  11. #include <stdio.h>
  12.  
  13. int main( void )
  14. {
  15.      struct bits {                                  /* Note 1 */
  16.           unsigned first     :1;
  17.           unsigned second    :1;
  18.           unsigned third     :1;
  19.           unsigned fourth    :1;
  20.           unsigned fifth     :1;
  21.           unsigned sixth     :1;
  22.           unsigned seventh   :1;
  23.           unsigned eighth    :1;
  24.           unsigned ninth     :1;
  25.           unsigned tenth     :1;
  26.           unsigned eleventh  :1;
  27.           unsigned twelfth   :1;
  28.           unsigned thirteenth:1;
  29.           unsigned fourteenth:1;
  30.           unsigned fifteenth :1;
  31.           unsigned sixteenth :1;
  32.      } ;
  33.  
  34.      struct bytes {                                 /* Note 2 */
  35.           unsigned first :8;
  36.           unsigned second:8;
  37.      } ;
  38.  
  39.      union access {                                 /* Note 3 */
  40.           short whole;
  41.           struct bits bit_access;
  42.           struct bytes byte_access;
  43.      } testvar;
  44.  
  45.                                                     /* Note 4 */
  46.      printf( "sizeof(struct bits)  %d.\n", sizeof( struct bits ) );
  47.      printf( "sizeof(struct bytes) %d.\n", sizeof( struct bytes ) );
  48.      printf( "sizeof(union access) %d.\n\n", sizeof(union access) );
  49.  
  50.      testvar.whole = 0x1248;                        /* Note 5 */
  51.  
  52.                                                     /* Note 6 */
  53.      printf( "first byte %x, second byte %x.\n\n", testvar.byte_access.first, testvar.byte_access.second );
  54.                                                     /* Note 7 */
  55.      printf( "Bits:\tfirst       %x,  second      %x,\n", testvar.bit_access.first, testvar.bit_access.second );
  56.      printf( "\tthird       %x,  fourth      %x,\n", testvar.bit_access.third, testvar.bit_access.fourth );
  57.      printf( "\tfifth       %x,  sixth       %x,\n", testvar.bit_access.fifth, testvar.bit_access.sixth );
  58.      printf( "\tseventh     %x,  eighth      %x,\n", testvar.bit_access.seventh, testvar.bit_access.eighth );
  59.      printf( "\tninth       %x,  tenth       %x,\n", testvar.bit_access.ninth, testvar.bit_access.tenth );
  60.      printf( "\televenth    %x,  twelfth     %x,\n", testvar.bit_access.eleventh, testvar.bit_access.twelfth );
  61.      printf( "\tthirteenth  %x,  fourteenth  %x,\n", testvar.bit_access.thirteenth, testvar.bit_access.fourteenth );
  62.      printf( "\tfifteenth   %x,  sixteenth   %x\n", testvar.bit_access.fifteenth, testvar.bit_access.sixteenth );
  63.      return 0;
  64. }