Hello,
I am trting to write a program that accepts numbers and count the number of bits in the given array of numbers. However my output is incorrect, I get the following
Enter nums:
10101010
The total count is 7
Code:
//Program that accepts a number and counts the bits.
#include <stdio.h>
int bitcnt(unsigned x);
int main (int argc, char *argv[]) {
unsigned x;
printf("Enter nums:\n");
scanf("%d", &x);
// bitcnt(x);
printf("The total count is %d\n", bitcnt(x));
return 0;
}
int bitcnt(unsigned x) {
int b;
for(b=0; b<x; x>>=1) {
if(x&01) {
b++;
}
}
// printf("The total count is %d\n", b);
return b;
}
All help is greatly appreciated.