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 / tbitstr.c < prev   
C/C++ Source or Header  |  1993-09-01  |  2KB  |  67 lines

  1. LISTING 11 - Illustrate Bitstr Objects
  2. /* tbitstr.c:  Test the Bitstr Interface */
  3.  
  4. #include <stdio.h>
  5. #include <assert.h>
  6. #include "bitstr.h"
  7.  
  8. #define NBITS 24
  9.  
  10. main()
  11. {
  12.     int i;
  13.     unsigned n;
  14.     Bitstr *bp = bitstr_create(NBITS);
  15.  
  16.     assert(bp);
  17.  
  18.     /* Set the even bits */
  19.     for (i = 0; i < NBITS; i += 2)
  20.         bitstr_set(bp,i);
  21.     bitstr_put(bp,stdout);
  22.     printf(" (%d)\n",bitstr_count(bp));
  23.  
  24.     /* Toggle the upper half */
  25.     for (i = NBITS/2; i < NBITS; ++i)
  26.         bitstr_toggle(bp,i);
  27.     bitstr_put(bp,stdout);
  28.     printf(" (%d)\n",bitstr_count(bp));
  29.  
  30.     /* Reset the lower half */
  31.     for (i = 0; i < NBITS/2; ++i)
  32.         bitstr_reset(bp,i);
  33.     bitstr_put(bp,stdout);
  34.     printf(" (%d)\n",bitstr_count(bp));
  35.  
  36.     /* Read a bit string */
  37.     fputs("Enter a bit string: ",stderr);
  38.     bitstr_put(bitstr_get(bp,stdin),stdout);
  39.     printf(" (%d)\n",bitstr_count(bp));
  40.  
  41.     printf("any? %d\n",bitstr_any(bp));
  42.     printf("test(0)? %d\n",bitstr_test(bp,0));
  43.  
  44.     bitstr_put(bitstr_toggle_all(bp),stdout);
  45.     printf(" (%d)\n",bitstr_count(bp));
  46.     bitstr_put(bitstr_reset_all(bp),stdout);
  47.     printf(" (%d)\n",bitstr_count(bp));
  48.     bitstr_put(bitstr_set_all(bp),stdout);
  49.     printf(" (%d)\n",bitstr_count(bp));
  50.  
  51.     return 0;
  52. }
  53.  
  54. /* Sample Execution:
  55. 101010101010101010101010 (12)
  56. 101010101010010101010101 (12)
  57. 000000000000010101010101 (6)
  58. Enter a bit string: 101001000100001
  59. 101001000100001000000000 (5)
  60. any? 1
  61. test(0)? 1
  62. 010110111011110111111111 (19)
  63. 000000000000000000000000 (0)
  64. 111111111111111111111111 (24)
  65. /*
  66.  
  67.