Advertisement
LightProgrammer000

Biwise [ | ]

Dec 1st, 2018
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.99 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define SET 32
  4.  
  5. // Prototipaçăo
  6. char *binbin(int a);
  7.  
  8. int main()
  9. {
  10.     int a;
  11.     int r;
  12.  
  13.     printf("\n Type a value from 0 to 255: ");
  14.     scanf("%d", &a);
  15.  
  16.     /*
  17.     - Analogia: Operador Bitwise(a | SET) ~ Operador Matemático (a + 32) = 97
  18.     | = Ou:
  19.     Exemplo: 65 -> 01000001
  20.              32 -> 00100000
  21.              97 -> 01100001
  22.     */
  23.     r = (a | SET);
  24.  
  25.     printf("\n -> %5s = %d \n", binbin(a), a);
  26.     printf("\n -> %5s = %d \n", binbin(SET), SET);
  27.     printf("\n -> %5s = %d \n", binbin(r), r);
  28.     printf("\n -> %d + %d = %d \n", a, SET, a + SET);
  29.  
  30.     return(0);
  31. }
  32.  
  33. // Apresentaçăo do formato binário
  34. char *binbin(int a)
  35. {
  36.     int i;
  37.     static char bin[8];
  38.  
  39.     for( i = 0; i < 8; i++ )
  40.     {
  41.         if( a & 0x80 )
  42.         {
  43.             bin[i] = '1';
  44.             a <<= 1;
  45.         }
  46.  
  47.         else
  48.         {
  49.             bin[i] = '0';
  50.             a <<= 1;
  51.         }
  52.     }
  53.  
  54.     bin[i] = '\0';
  55.  
  56.     return(bin);
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement