home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / cmplangm / 1989_4 / bit.c next >
C/C++ Source or Header  |  1988-01-05  |  7KB  |  162 lines

  1. /*
  2.     PROGRAM  : bit.c
  3.     AUTHOR   : John Gray 
  4.     DATE     : January 1988
  5.     FUNCTION : Demonstrates the use of 'bit strings' in C.
  6.  
  7. */
  8.  
  9. #include <stdio.h> 
  10.  
  11. #define HEX1           ((unsigned) (0x1))          /* hexadecimal 1          */
  12. #define BITSperBYTE    (8)                         /* # of bits per byte     */
  13. #define SIZE           (sizeof(unsigned) * BITSperBYTE)
  14.                                                    /* element size in bits   */  
  15.  
  16. #define SCRNsize       (80)                        /* # of char on 1 line    */
  17. #define LIMIT          (SCRNsize / SIZE)           /* max # of elements      */
  18.  
  19. #define BITrangeERR    printf("\n\7Bit out of range 1 - %d \n",value)
  20.  
  21. int value;                                         /* # of bits needed      */
  22.  
  23. main(){
  24.   int i,j,                                         /* indicies              */
  25.       max,                                         /* maximum array index   */
  26.       ch;                                          /* character input       */
  27.  
  28.   void Clear(), PrintBit(), ResetBit(), SetBit(), exit();
  29.  
  30.   printf("\nPlease enter number of bits [1 - %d] needed: ",LIMIT * SIZE);
  31.   scanf("%d", &value);
  32.   ch = getchar();                                  /* discard remaining \n  */
  33.   max = (value - 1) / SIZE;                        /* find max array index  */ 
  34.   if ( value > 0 && max < LIMIT )                  /* if within limits do   */
  35.     {
  36.     unsigned BitString[LIMIT];                     /* array of bits         */
  37.     Clear(BitString, max);                         /* reset all bits to 0   */ 
  38.     printf("\n\nCMD> ");
  39.     while ( (ch = getchar()) != EOF )              /* obtain user input     */
  40.       {
  41.       switch( ch | 32 )                                 
  42.         { 
  43.         case 'c':                                  /* call Clear            */
  44.           Clear(BitString, max);
  45.           break;
  46.  
  47.         case 'e':
  48.           exit(0);                                 /* exit to system        */
  49.  
  50.         case 'p':                                  /* call PrintBit         */
  51.           putchar('\n');
  52.           for ( i = max; i >= 0; i-- )
  53.             PrintBit(BitString[i], i);
  54.           putchar('\n');
  55.           break;
  56.  
  57.         case 's':                                  /* call SetBit           */
  58.           if ( GetBit(&i, &j) ) SetBit(&BitString[i], j);
  59.           else BITrangeERR;
  60.           break;
  61.  
  62.         case 'r':                                  /* call ResetBit         */
  63.           if ( GetBit(&i, &j) ) ResetBit(&BitString[i], j);
  64.           else BITrangeERR;
  65.           break;
  66.  
  67.         case 't':                                   /* call TestBit         */
  68.           if ( GetBit(&i, &j) )
  69.             printf("\nBit set is %c\n",(TestBit(BitString[i], j)) ? 'T' : 'F');
  70.           else BITrangeERR;
  71.           break;                              
  72.  
  73.         default:
  74.           printf("\nUsage C - clear, E - exit, P - print, R - reset, S - set, T - test\n");
  75.         }
  76.       ch = getchar();                               /*   clear remaining \n   */
  77.       printf("\nCMD> ");
  78.       }
  79.     }
  80.   else printf("\nValue entered is out of range.\n");
  81. }
  82.  
  83.  
  84. /*  Clear working bit string array to all 0's                                */
  85.  
  86. static void Clear(bs, m)
  87.   unsigned *bs;                                     /* array of bits         */ 
  88.   int      m;                                       /* max array index       */
  89. {
  90.   register int i;
  91.   for (i = 0; i <= m; i++, bs++)                    /* exclusive OR elmnts   */
  92.     *bs ^= *bs;                                     /* to set to all 0's     */
  93. }
  94.  
  95.  
  96. /* Obtain bit number from user, return elmnt index, and off set within elmnt */
  97.  
  98. static int GetBit(index, offset)
  99.   int *index,                                       /* index of array        */
  100.       *offset;                                      /* offset within elmnt   */
  101. {
  102.   int bit,                                          /* bit number to use     */
  103.       ok = 0;                                       /* assume false          */
  104.  
  105.   printf("\nPlease enter the bit # [1 - %d] ",value);
  106.   if ((ok = scanf("%d", &bit) &&
  107.        bit <= value && bit > 0)) {                  /* int and within range? */
  108.          *index = (bit - 1)/SIZE;                   /* find index            */
  109.          *offset = (bit % SIZE == 0) ? SIZE : bit % SIZE;
  110.   }                                                 /* find offset           */
  111.   return(ok);                                       /* return validity       */
  112.  
  113.  
  114. /*  Print valid bits of a given array element                                */ 
  115.  
  116. static void PrintBit(element, index)
  117.   unsigned element;                                 /* array element         */
  118.   int      index;                                   /* index of array elmnt  */ 
  119. {
  120.    register int i;
  121.  
  122.    for (i = SIZE; i > 0; i--)                       /* for each bit          */ 
  123.      if (index * SIZE + i <= value)                 /* if within range       */
  124.        putchar((element & (HEX1 << (i - 1))) ? '1' : '0');
  125. }
  126.  
  127.  
  128. /*  Set specified bit                                                        */
  129.  
  130. static void SetBit(element, offset)
  131.   unsigned *element;                                /* array element         */
  132.   int      offset;                                  /* offset within elmnt   */
  133. {                                                   /* OR original with 1    */
  134.   *element |= (HEX1 << offset - 1);                 /* shifted to crct pos   */
  135. }
  136.  
  137.  
  138. /*  Reset a specified bit                                                    */
  139.  
  140. static void ResetBit(element, offset)
  141.   unsigned *element;                                /* array element         */
  142.   int      offset;                                  /* offset within elmnt   */
  143. {
  144.   static unsigned mask = HEX1;                      /* 000...1               */
  145.  
  146.   mask <<= SIZE - 1;                                /* shift left  100...0   */
  147.   mask = ~(mask >> SIZE - offset);                  /* shift rt to crct pos  */ 
  148.   *element &= mask;                                 /* AND with original     */
  149. }
  150.  
  151.  
  152. /* Test specified bit, return non-zero if set else return zero               */
  153.  
  154. static int TestBit(element, offset)
  155.   unsigned element;                                 /* array element         */
  156.   int      offset;                                  /* offset within elmnt   */ 
  157. {
  158.   element <<= SIZE - offset;                        /* remove left portion   */
  159.   return(element >>= SIZE - 1);                     /* shift right           */
  160. }
  161.