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 / bitfld2.c < prev    next >
C/C++ Source or Header  |  2005-06-16  |  3KB  |  62 lines

  1. /*               bitfld2.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 in
  7.  *               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.      } *bit_access;
  33.  
  34.      struct bytes {                               /* Note 2 */
  35.           unsigned first:8;
  36.           unsigned second :8;
  37.      } *byte_access;
  38.  
  39.      unsigned short twobytes;                     /* Note 3 */
  40.                                                   /* Note 4 */
  41.      printf( "sizeof( struct bits )  %d.\n", sizeof( struct bits ) );
  42.      printf( "sizeof( struct bytes ) %d.\n", sizeof( struct bytes ));
  43.      printf( "sizeof( twobytes )     %d.\n\n", sizeof( twobytes ) );
  44.  
  45.      twobytes = 0x1248;                           /* Note 5 */
  46.      byte_access = ( struct bytes * ) &twobytes;  /* Note 6 */
  47.      bit_access  = ( struct bits * )  &twobytes;
  48.  
  49.                                                   /* Note 7 */
  50.      printf( "first byte %x, second byte %x.\n\n", byte_access->first, byte_access->second );
  51.                                                   /* Note 8 */
  52.      printf( "Bits:\tfirst       %x,  second      %x,\n", bit_access->first, bit_access->second );
  53.      printf( "\tthird       %x,  fourth      %x,\n", bit_access->third, bit_access->fourth );
  54.      printf( "\tfifth       %x,  sixth       %x,\n", bit_access->fifth, bit_access->sixth );
  55.      printf( "\tseventh     %x,  eighth      %x,\n", bit_access->seventh, bit_access->eighth );
  56.      printf( "\tninth       %x,  tenth       %x,\n", bit_access->ninth, bit_access->tenth );
  57.      printf( "\televenth    %x,  twelfth     %x,\n", bit_access->eleventh, bit_access->twelfth );
  58.      printf( "\tthirteenth  %x,  fourteenth  %x,\n", bit_access->thirteenth, bit_access->fourteenth );
  59.      printf( "\tfifteenth   %x,  sixteenth   %x\n", bit_access->fifteenth, bit_access->sixteenth );
  60.      return 0;
  61. }
  62.