2.9.c 398 B

1234567891011121314151617181920212223242526272829303132
  1. int oldbitcount(unsigned x);
  2. int newbitcount(unsigned x);
  3. int main()
  4. {
  5. return 0;
  6. }
  7. /* bitcount: count 1 bits in x */
  8. int oldbitcount(unsigned x)
  9. {
  10. int b;
  11. for (b = 0; x != 0; x >>= 1)
  12. if (x & 01)
  13. b++;
  14. return b;
  15. }
  16. /* bitcount: count 1 bits in x */
  17. int newbitcount(unsigned x)
  18. {
  19. int b=0;
  20. for (int b=0; x!=0;)
  21. {
  22. x &= (x-1);
  23. b++;
  24. }
  25. return b;
  26. }