Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.60 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. const int m1  = 1431655765; // binary: 0101...
  4. const int m2  = 858993459; // binary: 00110011...
  5. const int m4  = 252645135; // binary: 0000111100001111...
  6.  
  7. int main()
  8. {
  9.   int a, b;
  10.  
  11.   scanf("%d %d", &a, &b);
  12.  
  13.   int result = a ^ b;
  14.  
  15.   result = (result & m1 ) + ((result >>  1) & m1 ); //put count of each  2 bits into those  2 bits
  16.   result = (result & m2 ) + ((result >>  2) & m2 ); //put count of each  4 bits into those  4 bits
  17.   result = (result & m4 ) + ((result >>  4) & m4 ); //put count of each  8 bits into those  8 bits
  18.  
  19.   printf("%d", 7 - result);
  20.  
  21.   return 0;
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement