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

  1. LISTING 8 - Illustrates Bits Objects
  2. /* tbits.c:  Test the Bits Interface */
  3.  
  4. #include <stdio.h>
  5. #include <assert.h>
  6. #include "bits.h"
  7.  
  8. #define NBITS 24
  9.  
  10. main()
  11. {
  12.     int i;
  13.     unsigned n;
  14.     Bits *bp = bits_create(NBITS);
  15.  
  16.     assert(bp);
  17.  
  18.     /* Set the even bits */
  19.     for (i = 0; i < NBITS; i += 2)
  20.         bits_set(bp,i);
  21.     bits_put(bp,stdout);
  22.     printf(" (%d)\n",bits_count(bp));
  23.  
  24.     /* Toggle the upper half */
  25.     for (i = NBITS/2; i < NBITS; ++i)
  26.         bits_toggle(bp,i);
  27.     bits_put(bp,stdout);
  28.     printf(" (%d)\n",bits_count(bp));
  29.  
  30.     /* Reset the lower half */
  31.     for (i = 0; i < NBITS/2; ++i)
  32.         bits_reset(bp,i);
  33.     bits_put(bp,stdout);
  34.     printf(" (%d)\n",bits_count(bp));
  35.  
  36.     /* Read a bit string */
  37.     fputs("Enter a bit string: ",stderr);
  38.     bits_put(bits_get(bp,stdin),stdout);
  39.     printf(" (%d)\n",bits_count(bp));
  40.  
  41.     /* Convert to and from unsigned */
  42.     n = bits_to_uint(bp);
  43.     printf("n: %u\n",n);
  44.     bp = bits_from_uint(bp,n);
  45.     bits_put(bp,stdout);
  46.     printf(" (%d)\n",bits_count(bp));
  47.  
  48.     /* Test any() and test() */
  49.     printf("any? %d\n",bits_any(bp));
  50.     printf("test(0)? %d\n",bits_test(bp,0));
  51.  
  52.     /* Test toggle and reset */
  53.     bits_put(bits_toggle_all(bp),stdout);
  54.     printf(" (%d)\n",bits_count(bp));
  55.     bits_put(bits_reset_all(bp),stdout);
  56.     printf(" (%d)\n",bits_count(bp));
  57.     bits_put(bits_set_all(bp),stdout);
  58.     printf(" (%d)\n",bits_count(bp));
  59.  
  60.     return 0;
  61. }
  62.  
  63. /* Sample Execution:
  64. 010101010101010101010101 (12)
  65. 101010101010010101010101 (12)
  66. 101010101010000000000000 (6)
  67. Enter a bit string: 101001000100001
  68. 000000000101001000100001 (5)
  69. n: 21025
  70. 000000000101001000100001 (5)
  71. any? 1
  72. test(0)? 1
  73. 111111111010110111011110 (19)
  74. 000000000000000000000000 (0)
  75. 111111111111111111111111 (24)
  76.