fork(1) download
  1. #include <stdio.h>
  2.  
  3. unsigned int bits0(unsigned int num) {
  4. int count = 0; while(num) num >>=1, ++count; return count;
  5. }
  6. unsigned int bits1(unsigned int x) {
  7. int y, m=0, n=0;
  8.  
  9. y = x - 0x100;
  10. m = (y >> 16) & 8;
  11. n = n + m;
  12. x = x << m;
  13.  
  14. y = x - 0x1000;
  15. m = (y >> 16) & 4;
  16. n = n + m;
  17. x = x << m;
  18.  
  19. y = x - 0x4000;
  20. m = (y >> 16) & 2;
  21. n = n + m;
  22. x = x << m;
  23.  
  24. y = x >> 14;
  25. m = y & ~(y >> 1);
  26. return 14 - n + m;
  27. }
  28. int main(int argc, char **argv) {
  29. unsigned int j=1<<12,i=0;
  30. for (;i<10;++i,j<<=1){
  31. unsigned int d = j|1;
  32. printf("%lu==%lu\n", bits0(d), bits1(d));
  33. }
  34. for (i=1;i<65536;++i)
  35. if(bits0(i)!=bits1(i))
  36. printf("ERROR i=%d %lu!=%lu\n", bits0(i), bits1(i));
  37.  
  38. return 0;
  39. }
Success #stdin #stdout 0s 2292KB
stdin
Standard input is empty
stdout
13==13
14==14
15==15
16==16
17==18
18==22
19==76
20==2056
21==1
22==1