fork(2) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int bitCount(unsigned int n) {
  5.  
  6. int counter = 0;
  7. while(n) {
  8. counter ++;
  9. n &= (n - 1);
  10. }
  11. return counter;
  12. }
  13.  
  14. int main() {
  15. printf ("# 1-bits in base 2 representation of %u = %d, should be 1\n",
  16. 0b10000, bitCount (0b10000));
  17. printf ("# 1-bits in base 2 representation of %u = %d, should be 0\n",
  18. 0, bitCount (0));
  19. printf ("# 1-bits in base 2 representation of %u = %d, should be 1\n",
  20. 1, bitCount (1));
  21. printf ("# 1-bits in base 2 representation of %u = %d, should be 16\n",
  22. 2863311530u, bitCount (2863311530u));
  23. printf ("# 1-bits in base 2 representation of %u = %d, should be 1\n",
  24. 536870912, bitCount (536870912));
  25. printf ("# 1-bits in base 2 representation of %u = %d, should be 32\n",
  26. 4294967295u, bitCount (4294967295u));
  27. return 0;
  28. }
Success #stdin #stdout 0.01s 5348KB
stdin
Standard input is empty
stdout
# 1-bits in base 2 representation of 16 = 1, should be 1
# 1-bits in base 2 representation of 0 = 0, should be 0
# 1-bits in base 2 representation of 1 = 1, should be 1
# 1-bits in base 2 representation of 2863311530 = 16, should be 16
# 1-bits in base 2 representation of 536870912 = 1, should be 1
# 1-bits in base 2 representation of 4294967295 = 32, should be 32