Advertisement
LightProgrammer000

Biwise [ & ]

Dec 1st, 2018
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.81 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define SET 223
  4.  
  5. // Prototipação
  6. char *binbin(int a);
  7.  
  8. // Mascaramento de Bits
  9. int main()
  10. {
  11.     int a;
  12.     int r;
  13.  
  14.     printf("\n Type a value from 0 to 255: ");
  15.     scanf("%d", &a);
  16.  
  17.     // &: Operador BitWise
  18.     r = a & SET;
  19.  
  20.     printf("\n -> %5s = %d \n", binbin(a), a);
  21.     printf("\n -> %5s = %d \n", binbin(SET), SET);
  22.     printf("\n -> %5s = %d \n", binbin(r), r);
  23.     //printf("\n --> %d + %d = %d \n", a, SET, a + SET);
  24.  
  25.     return(0);
  26. }
  27.  
  28. char *binbin(int a)
  29. {
  30.     int i;
  31.     static char bin[9];
  32.  
  33.     for( i = 0; i < 8; i++ )
  34.     {
  35.         if( a & 0x80 )
  36.         {
  37.             bin[i] = '1';
  38.             a <<= 1;
  39.         }
  40.  
  41.         else
  42.         {
  43.             bin[i] = '0';
  44.             a <<= 1;
  45.         }
  46.     }
  47.     bin[i] = '\0';
  48.  
  49.     return(bin);
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement