home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / qc_prog / chap07 / bitwise.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-04-05  |  2.3 KB  |  96 lines

  1. /* bitwise.c -- demonstrate the bitwise operators */
  2.  
  3. #include <stdio.h>
  4.  
  5. main()
  6. {
  7.     unsigned int val1, val2, result;
  8.     int ch;
  9.     extern void show();
  10.  
  11.     while(1)
  12.         {
  13.         printf("\nval1: ");
  14.         if (scanf("%d", &val1) != 1)
  15.             break;
  16.  
  17.         printf("val2: ");
  18.         if (scanf("%d", &val2) != 1)
  19.             break;
  20.  
  21.         printf("\tval1   = ");
  22.         show(val1);
  23.         printf("\tval2   = ");
  24.         show(val2);
  25.  
  26.         printf("Bitwise Operator: ");
  27.         while ((ch = getchar()) == '\n')
  28.             {
  29.             continue;
  30.             }
  31.         if (ch == EOF)
  32.             break;
  33.         switch (ch)
  34.             {
  35.             case '&':
  36.                 result = val1 & val2;
  37.                 printf("Executing: result = val1 & val2;\n");
  38.                 break;
  39.             case '|':
  40.                 result = val1 |= val2;
  41.                 printf("Executing: result = val1 | val2;\n");
  42.                 break;
  43.             case '^':
  44.                 result = val1 ^= val2;
  45.                 printf("Executing: result = val1 ^ val2;\n");
  46.                 break;
  47.             case '~':
  48.                 result = ~val1;
  49.                 printf("Executing: result = ~val1;\n");
  50.                 printf("\tresult = ");
  51.                 show(result);
  52.                 result = ~val2;
  53.                 printf("Executing: result = ~val2;\n");
  54.                 break;
  55.             case '<':
  56.                 result = val1 <<= val2;
  57.                 printf("Executing: result = val1 <<val2;\n");
  58.                 break;
  59.             case '>':
  60.                 result = val1 >>= val2;
  61.                 printf("Executing: result = val1 >>val2;\n");
  62.                 break;
  63.             case 'q':
  64.             case 'Q':
  65.                 return(0);
  66.               default:
  67.                 continue;
  68.             }
  69.         printf("\tresult = ");
  70.         show(result);
  71.         }
  72. }
  73.  
  74. void bitout(unsigned char num[])
  75. {
  76.     int bytes = 2, i, j;
  77.  
  78.     /* IBM PC stores ints low/hi. */
  79.     for (i = bytes-1; i >= 0; --i)
  80.         {
  81.         for (j = 7; j >= 0; --j)
  82.             {
  83.             putchar((num[i]&(1<<j))?'1':'0');
  84.             }
  85.         }
  86. }
  87.  
  88. void show(unsigned int val)
  89. {
  90.     extern void bitout();
  91.  
  92.     printf("(%05u decimal)", val);
  93.     bitout(&val);
  94.     printf(" binary\n");
  95. }
  96.