Answer:
// program in C.
// header
#include <stdio.h>
// main function
int main()
{
// variables
unsigned int n;
unsigned int count = 0;
// ask to enter an unsigned integer
printf("Enter an unsigned integer:");
// read integer
scanf("%d",&n);
int nn=n;
// find the set bits
while (n)
{
count += n & 1;
n >>= 1;
}
// print set bits
printf("number of 1bits in number %d are: %d ",nn,count);
return 0;
}
Step-by-step explanation:
Read an unsigned integer from user and assign it to variable "n".Initialize a variable "count=0" to store the count of set bits.Loop through all the bits of number,If bits is set the increment the count by 1.To check a bit is set, find "bitwise and" of number with 1.then right shift the number by 1. Repeat this for all the bits of number (until n equals to 0).
Output:
Enter an unsigned integer:15
number of 1bits in number 15 are: 4