home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / CMPLNGMG / CL89APR1.ARC / BIT.C next >
Encoding:
C/C++ Source or Header  |  1988-01-05  |  6.7 KB  |  162 lines

  1. /*
  2.  
  3.     PROGRAM  : bit.c
  4.  
  5.     AUTHOR   : John Gray 
  6.  
  7.     DATE     : January 1988
  8.  
  9.     FUNCTION : Demonstrates the use of 'bit strings' in C.
  10.  
  11.  
  12.  
  13. */
  14.  
  15.  
  16.  
  17. #include <stdio.h> 
  18.  
  19.  
  20.  
  21. #define HEX1           ((unsigned) (0x1))          /* hexadecimal 1          */
  22.  
  23. #define BITSperBYTE    (8)                         /* # of bits per byte     */
  24.  
  25. #define SIZE           (sizeof(unsigned) * BITSperBYTE)
  26.  
  27.                                                    /* element size in bits   */  
  28.  
  29.  
  30.  
  31. #define SCRNsize       (80)                        /* # of char on 1 line    */
  32.  
  33. #define LIMIT          (SCRNsize / SIZE)           /* max # of elements      */
  34.  
  35.  
  36.  
  37. #define BITrangeERR    printf("\n\7Bit out of range 1 - %d \n",value)
  38.  
  39.  
  40.  
  41. int value;                                         /* # of bits needed      */
  42.  
  43.  
  44.  
  45. main(){
  46.  
  47.   int i,j,                                         /* indicies              */
  48.  
  49.       max,                                         /* maximum array index   */
  50.  
  51.       ch;                                          /* character input       */
  52.  
  53.  
  54.  
  55.   void Clear(), PrintBit(), ResetBit(), SetBit(), exit();
  56.  
  57.  
  58.  
  59.   printf("\nPlease enter number of bits [1 - %d] needed: ",LIMIT * SIZE);
  60.  
  61.   scanf("%d", &value);
  62.  
  63.   ch = getchar();                                  /* discard remaining \n  */
  64.  
  65.   max = (value - 1) / SIZE;                        /* find max array index  */ 
  66.  
  67.   if ( value > 0 && max < LIMIT )                  /* if within limits do   */
  68.  
  69.     {
  70.  
  71.     unsigned BitString[LIMIT];                     /* array of bits         */
  72.  
  73.     Clear(BitString, max);                         /* reset all bits to 0   */ 
  74.  
  75.     printf("\n\nCMD> ");
  76.  
  77.     while ( (ch = getchar()) != EOF )              /* obtain user input     */
  78.  
  79.       {
  80.  
  81.       switch( ch | 32 )                                 
  82.  
  83.         { 
  84.  
  85.         case 'c':                                  /* call Clear            */
  86.  
  87.           Clear(BitString, max);
  88.  
  89.           break;
  90.  
  91.  
  92.  
  93.         case 'e':
  94.  
  95.           exit(0);                                 /* exit to system        */
  96.  
  97.  
  98.  
  99.         case 'p':                                  /* call PrintBit         */
  100.  
  101.           putchar('\n');
  102.  
  103.           for ( i = max; i >= 0; i-- )
  104.  
  105.             PrintBit(BitString[i], i);
  106.  
  107.           putchar('\n');
  108.  
  109.           break;
  110.  
  111.  
  112.  
  113.         case 's':                                  /* call SetBit           */
  114.  
  115.           if ( GetBit(&i, &j) ) SetBit(&BitString[i], j);
  116.  
  117.           else BITrangeERR;
  118.  
  119.           break;
  120.  
  121.  
  122.  
  123.         case 'r':                                  /* call ResetBit         */
  124.  
  125.           if ( GetBit(&i, &j) ) ResetBit(&BitString[i], j);
  126.  
  127.           else BITrangeERR;
  128.  
  129.           break;
  130.  
  131.  
  132.  
  133.         case 't':                                   /* call TestBit         */
  134.  
  135.           if ( GetBit(&i, &j) )
  136.  
  137.             printf("\nBit set is %c\n",(TestBit(BitString[i], j)) ? 'T' : 'F');
  138.  
  139.           else BITrangeERR;
  140.  
  141.           break;                              
  142.  
  143.  
  144.  
  145.         default:
  146.  
  147.           printf("\nUsage C - clear, E - exit, P - print, R - reset, S - set, T - test\n");
  148.  
  149.         }
  150.  
  151.       ch = getchar();                               /*   clear remaining \n   */
  152.  
  153.       printf("\nCMD> ");
  154.  
  155.       }
  156.  
  157.     }
  158.  
  159.   else printf("\nValue entered is out of range.\n");
  160.  
  161. }
  162.  
  163.  
  164.  
  165.  
  166.  
  167. /*  Clear working bit string array to all 0's                                */
  168.  
  169.  
  170.  
  171. static void Clear(bs, m)
  172.  
  173.   unsigned *bs;                                     /* array of bits         */ 
  174.  
  175.   int      m;                                       /* max array index       */
  176.  
  177. {
  178.  
  179.   register int i;
  180.  
  181.   for (i = 0; i <= m; i++, bs++)                    /* exclusive OR elmnts   */
  182.  
  183.     *bs ^= *bs;                                     /* to set to all 0's     */
  184.  
  185. }
  186.  
  187.  
  188.  
  189.  
  190.  
  191. /* Obtain bit number from user, return elmnt index, and off set within elmnt */
  192.  
  193.  
  194.  
  195. static int GetBit(index, offset)
  196.  
  197.   int *index,                                       /* index of array        */
  198.  
  199.       *offset;                                      /* offset within elmnt   */
  200.  
  201. {
  202.  
  203.   int bit,                                          /* bit number to use     */
  204.  
  205.       ok = 0;                                       /* assume false          */
  206.  
  207.  
  208.  
  209.   printf("\nPlease enter the bit # [1 - %d] ",value);
  210.  
  211.   if ((ok = scanf("%d", &bit) &&
  212.  
  213.        bit <= value && bit > 0)) {                  /* int and within range? */
  214.  
  215.          *index = (bit - 1)/SIZE;                   /* find index            */
  216.  
  217.          *offset = (bit % SIZE == 0) ? SIZE : bit % SIZE;
  218.  
  219.   }                                                 /* find offset           */
  220.  
  221.   return(ok);                                       /* return validity       */
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228. /*  Print valid bits of a given array element                                */ 
  229.  
  230.  
  231.  
  232. static void PrintBit(element, index)
  233.  
  234.   unsigned element;                                 /* array element         */
  235.  
  236.   int      index;                                   /* index of array elmnt  */ 
  237.  
  238. {
  239.  
  240.    register int i;
  241.  
  242.  
  243.  
  244.    for (i = SIZE; i > 0; i--)                       /* for each bit          */ 
  245.  
  246.      if (index * SIZE + i <= value)                 /* if within range       */
  247.  
  248.        putchar((element & (HEX1 << (i - 1))) ? '1' : '0');
  249.  
  250. }
  251.  
  252.  
  253.  
  254.  
  255.  
  256. /*  Set specified bit                                                        */
  257.  
  258.  
  259.  
  260. static void SetBit(element, offset)
  261.  
  262.   unsigned *element;                                /* array element         */
  263.  
  264.   int      offset;                                  /* offset within elmnt   */
  265.  
  266. {                                                   /* OR original with 1    */
  267.  
  268.   *element |= (HEX1 << offset - 1);                 /* shifted to crct pos   */
  269.  
  270. }
  271.  
  272.  
  273.  
  274.  
  275.  
  276. /*  Reset a specified bit                                                    */
  277.  
  278.  
  279.  
  280. static void ResetBit(element, offset)
  281.  
  282.   unsigned *element;                                /* array element         */
  283.  
  284.   int      offset;                                  /* offset within elmnt   */
  285.  
  286. {
  287.  
  288.   static unsigned mask = HEX1;                      /* 000...1               */
  289.  
  290.  
  291.  
  292.   mask <<= SIZE - 1;                                /* shift left  100...0   */
  293.  
  294.   mask = ~(mask >> SIZE - offset);                  /* shift rt to crct pos  */ 
  295.  
  296.   *element &= mask;                                 /* AND with original     */
  297.  
  298. }
  299.  
  300.  
  301.  
  302.  
  303.  
  304. /* Test specified bit, return non-zero if set else return zero               */
  305.  
  306.  
  307.  
  308. static int TestBit(element, offset)
  309.  
  310.   unsigned element;                                 /* array element         */
  311.  
  312.   int      offset;                                  /* offset within elmnt   */ 
  313.  
  314. {
  315.  
  316.   element <<= SIZE - offset;                        /* remove left portion   */
  317.  
  318.   return(element >>= SIZE - 1);                     /* shift right           */
  319.  
  320. }
  321.  
  322.