Write a function named bitcount () in bitcount.c that returns the number of 1-bi
ID: 3789151 • 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. Remember to fill in the identification information and run the completed program to verify correctness./* Name: Lab section time: */#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 %u = %d, should be l ", 1, bitCount (1)); prin f ("# 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;} int bitCount (unsigned int n) {/* your code here */} You have decided that you want your bitcount program above to work from the command-line (see K&R; Sec. 5.10), as follows: # ./bitcount 17 2 # ./bitcount 255 8 # ./bitcount 10 20 too many arguments! # ./bitcount [the same result as from problem 4] You may assume that the single argument will always be an integer in the range from 0 to 2^31-1. You will find the function atoi helpful.Explanation / Answer
Problem 4"
#include<stdio.h>
int bitCount(unsigned int n)
{
int counter = 0;
while(n) {
counter += n % 2;
n >>= 1;
}
return counter;
}
int main(int argc, char const *argv[])
{
/* code */
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;
return 0;
}
=============================================
Output:
akshay@akshay-Inspiron-3537:~/Chegg$ gcc bit
bitcount.c bit.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ gcc bitcount.c
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
# 1-bits in base 2 representation of 0 = 0, should be 0
# 1-bits in base 2 representation of 1 = 1, should be 1
# 1-bits in base 2 representation of 2863311530 = 16, should be 16
# 1-bits in base 2 representation of 536870912 = 1, should be 1
# 1-bits in base 2 representation of 4294967295 = 32, should be 32
===================================================
problem 5:
#include<stdio.h>
int bitCount(unsigned int n)
{
int counter = 0;
while(n) {
counter += n % 2;
n >>= 1;
}
return counter;
}
int main(int argc, char const *argv[])
{
/* code */
unsigned int n=atoi(argv[1]);
if(argv[2])
{
printf("Too many arguments ");
}
else
{
printf ("# 1-bits in base 2 representation of %d = %d, should be 2 ",
n, bitCount (n));
}
return 0;
}
============================================
akshay@akshay-Inspiron-3537:~/Chegg$ gcc bitcount.c
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out 3
# 1-bits in base 2 representation of 3 = 2, should be 2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.