Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Write an efficient C program to count the number of bits set in an
- unsigned integer.
- i/p o/p
- ==== ===
- 0(00) 0
- 1(01) 1
- 2(10) 1
- 3(11) 2
- ..... ...
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- int main(int argc, char **argv)
- {
- int x, i, j, num, factor;
- if(argc <2){
- printf ("Usage ./bitcount [numbers to process]\n");
- return 0;}
- for(i=1; i<argc ;i++) {
- num=atoi(argv[i]);
- x=num;
- j=0;
- while (x>0){
- factor= sqrt(x);
- x-=pow(2, factor);
- j++;
- }
- printf ("%d : %d\n", num, j);
- }
- }
Add Comment
Please, Sign In to add comment