home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / pcmag / vol7n08.zip / BITTEST.C next >
C/C++ Source or Header  |  1988-04-26  |  2KB  |  83 lines

  1. /* bittest.c
  2.  * tests bit-map routines
  3.  */
  4.  
  5. #include<stdio.h>
  6. #include<mem.h>
  7. #include"PcMag.h"                        /* standard definitions    */
  8.  
  9. #define BITMAP_SIZE 1024
  10. #define NUM_BITS    8
  11. #define MAXVAL   (BITMAP_SIZE*NUM_BITS)
  12. #define Bmap_init(bitmap,size)   memset(bitmap,0,size)
  13.  
  14. unsigned char bitmap[BITMAP_SIZE];       /* can track 8000 elements */
  15.  
  16. main()
  17. {
  18.     int n,i;
  19.     char    linebuffer[100];
  20.  
  21.     Bmap_init(bitmap,BITMAP_SIZE);      /* initialize the bitmap to 0   */
  22.  
  23.     while(TRUE)
  24.     {
  25.         printf("\nEnter a number to put in the list & <RETURN> (^Z to go on):\n");
  26.         if(!gets(linebuffer))
  27.             break;
  28.         i = atoi(linebuffer);
  29.         if(i < 0 || i > MAXVAL)
  30.             printf("\nOnly numbers from 0 to %d are valid in this demo",MAXVAL);
  31.         else
  32.             setbit(bitmap,i);
  33.     }
  34.  
  35.     while(TRUE)
  36.     {
  37.         printf("\nEnter a number to EXCLUDE from the list & <RETURN> (^Z to go on):\n");
  38.         if(!gets(linebuffer))
  39.             break;
  40.         i = atoi(linebuffer);
  41.         if(i < 0 || i > MAXVAL)
  42.             printf("\nOnly numbers from 0 to %d are valid in this demo",MAXVAL);
  43.         else
  44.             resetbit(bitmap,i);
  45.     }
  46.  
  47.     printf("\nThe included numbers are:\n");
  48.  
  49.     for(i = n = 0; i < MAXVAL; i++)
  50.         if(isbitset(bitmap,i))      {
  51.             n++;
  52.             printf("%3d %c",i, ((n % 13) ? ' ' : '\n') );
  53.         }
  54.     printf("\nThat\'s it");
  55. }
  56.  
  57.     /* setbit() - turns ON bit n in bitmap      */
  58. setbit(bitmap,n)    
  59. char    *bitmap;
  60. int     n;
  61. {
  62.     bitmap[n/8] |= (1 << (n % 8));
  63. }
  64.  
  65.     /* unsetbit()  - turns OFF bit n in bitmap  */
  66. resetbit(bitmap,n)
  67. char    *bitmap;
  68. int     n;
  69. {
  70.     bitmap[n/8] &= (~(1 << (n % 8)));
  71. }
  72.  
  73. /* isbitset() - returns TRUE if bit n is ON in bitmap, else returns FALSE */
  74.  
  75. isbitset(bitmap,n)
  76. char    *bitmap;
  77. int     n;
  78. {
  79.     return (bitmap[n/8] & (1 << (n%8)));
  80. }
  81.  
  82.     /* End of Btest.c ****************************/
  83.