Write a function named bitCount( in bitcount.c that returns the number of 1-bits
ID: 3586715 • Letter: W
Question
Write a function named bitCount( in bitcount.c that returns the number of 1-bits in the binary representation of its unsigned integer argument. For example, 59-Ob011 1011. The number of 1-bits of 59 is 5 #include int bitCount (unsigned int n); int main ) printf ("# 1-bits in base 2 representation of %u = ød, should be 0 ", 0, bitCount (0)); printf ("# 1-bits in base 2 representation of = sd, should be 1 ", 1, bitCount (1)); printf ("# 1-bits in base 2 representation of %u = sd, should be 161n", 2863311530u, bitCount (2863311530u); printf ("# 1-bits in base 2 representation of = sd, should be 1 ", 536870912, bitCount (536870912)) printf ("# 1-bits in base 2 representation of sd, should be 32 ", 4294967295u, bitCount (4294967295u)); return 0; int bitCount (unsigned int n) /*your code here */Explanation / Answer
bitcount.c :
#include<stdio.h>
int bitCount(unsigned int n);
int main(){
printf("# 1-bits in base 2 representation of %u = %d, should be 0 ",0,bitCount(0));
printf("# 1-bits in base 2 representation of %u = %d, should be 1 ",1,bitCount(1));
printf("# 1-bits in base 2 representation of %u = %d, should be 16 ",2863311530u,bitCount(2863311530u));
printf("# 1-bits in base 2 representation of %u = %d, should be 1 ",536870912,bitCount(536870912));
printf("# 1-bits in base 2 representation of %u = %d, should be 32 ",4294967295u,bitCount(4294967295u));
return 0;
}
//Below function will count the no of 1-bits in binary representation of unsigned integer
int bitCount(unsigned int n){
int>
while(n!=0){
r = n % 2;//remainder bit
if(r == 1){
oneBits++;
}
n /= 2;
}
return oneBits;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.