Give a method to count the number of ones in a 32 bit number?

Answer Posted / jayaprakash

#include<stdio.h>
#include<conio.h>

main()
{
int i;
int n;
int count=0;
int j;
int res=0;
clrscr();
printf("Enter the number:");
scanf("%d",&n);
for(j=15;j>=0;j--)
{ i=1;
i=i<<j;
res=i&n;
if(res!=0)
count++;
}
printf("\nNumber of ones is:%d",count);


getch();

}

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Are the variables argc and argv are local to main?

778


What is the general form of a C program?

590


When was c language developed?

687


How do you view the path?

652


Is stack a keyword in c?

624






Can you add pointers together? Why would you?

628


Explain what is the most efficient way to store flag values?

686


Can you please explain the difference between strcpy() and memcpy() function?

588


Should I use symbolic names like true and false for boolean constants, or plain 1 and 0?

587


What are operators in c?

571


What is the difference between ++a and a++?

678


how to print electricity bill according to following charges first 100 units -1rs per unit for next 200 units-1.50 rs per unit without using conditions

2710


What is logical error?

594


What is the default value of local and global variables in c?

551


Describe explain how arrays can be passed to a user defined function

593