Advertisement
levartolona

C_PRG_LANG_EX_2.9

Jan 26th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.38 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. // Count amount of '1' bits
  4. int bitcount(unsigned int x)
  5. {
  6.     int cnt ;
  7.  
  8.     for (cnt = 0;  x != 0; x &= (x - 1))
  9.         cnt++;
  10.  
  11.     return cnt;
  12. }
  13.  
  14. int main(void)
  15. {
  16.     int x, y;
  17.     x = 2679;
  18.     y = 1473;
  19.  
  20.     printf("bitcount for %i is %i\n", x, bitcount(x));
  21.     printf("bitcount for %i is %i\n", y, bitcount(y));
  22.  
  23.     return 0;
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement