Guest User

Untitled

a guest
Dec 4th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. int bitcount(int n) {
  2. unsigned int c1 = 0;
  3. for (; n > 0; n >>= 1)
  4. if (n & 1) {
  5. c1++;
  6. }
  7. return c1;
  8. }
  9.  
  10. int main() {
  11. int a = -2;
  12. printf("%u n", bitcount(a));
  13. return 0;
  14. }
  15.  
  16. int bits(unsigned int x)
  17. {
  18. x = x - ((x >> 1) & 0x55555555);
  19. x = (x & 0x33333333) + ((x>>2)&0x33333333);
  20. x = (x + (x >> 4)) & 0x0F0F0F0F;
  21. x += x >> 8;
  22. x += x >> 16;
  23. return x&0x3F;
  24. }
  25.  
  26. int main()
  27. {
  28. for(int i = -5; i <= 5; ++i)
  29. cout << setw(3) << i << setw(6) << bits(i) << endl;
  30. }
Add Comment
Please, Sign In to add comment