Guest User

Untitled

a guest
Jul 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. /*
  2.  
  3. Write an efficient C program to count the number of bits set in an
  4. unsigned integer.
  5. i/p o/p
  6. ==== ===
  7. 0(00) 0
  8. 1(01) 1
  9. 2(10) 1
  10. 3(11) 2
  11. ..... ...
  12.  
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <math.h>
  18.  
  19. int main(int argc, char **argv)
  20. {
  21. int x, i, j, num, factor;
  22. if(argc <2){
  23. printf ("Usage ./bitcount [numbers to process]\n");
  24. return 0;}
  25. for(i=1; i<argc ;i++) {
  26. num=atoi(argv[i]);
  27. x=num;
  28. j=0;
  29. while (x>0){
  30. factor= sqrt(x);
  31. x-=pow(2, factor);
  32. j++;
  33. }
  34. printf ("%d : %d\n", num, j);
  35. }
  36. }
Add Comment
Please, Sign In to add comment