home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_11_11 / allison / tbit.c < prev    next >
C/C++ Source or Header  |  1993-09-01  |  1KB  |  52 lines

  1. LISTING 5 - Illustrates the Bit Access Functions from bit.h
  2. /* tbit.c:  Use bit operations from bit.h */
  3.  
  4. #include <stdio.h>
  5. #include "bit.h"
  6.  
  7. main()
  8. {
  9.     int i;
  10.     unsigned n = 0;
  11.     size_t nb = nbits(n);
  12.  
  13.     /* Set the even bits */
  14.     for (i = 0; i < nb; i += 2)
  15.         n = set(n,i);
  16.     printf("n == %04X (",n);
  17.     fputb(n,stdout);
  18.     printf("), count == %d\n",count(n));
  19.  
  20.     /* Toggle the upper half */
  21.     for (i = nb/2; i < nb; ++i)
  22.         n = toggle(n,i);
  23.     printf("n == %04X (",n);
  24.     fputb(n,stdout);
  25.     printf("), count == %d\n",count(n));
  26.  
  27.     /* Reset the lower half */
  28.     for (i = 0; i < nb/2; ++i)
  29.         n = reset(n,i);
  30.     printf("n == %04X (",n);
  31.     fputb(n,stdout);
  32.     printf("), count == %d\n",count(n));
  33.  
  34.     /* Read a bit string */
  35.     fputs("Enter a bit string: ",stderr);
  36.     n = fgetb(stdin);
  37.     printf("n == %04X (",n);
  38.     fputb(n,stdout);
  39.     printf("), count == %d\n",count(n));
  40.  
  41.     return 0;
  42. }
  43.  
  44. /* Sample Execution:
  45.  
  46. n == 5555 (0101010101010101), count == 8
  47. n == AA55 (1010101001010101), count == 8
  48. n == AA00 (1010101000000000), count == 4
  49. Enter a bit string: 101001000100001
  50. n == 5221 (0101001000100001), count == 5
  51. /*
  52.