What is the most efficient way to count the number of bits
which are set in a value?
Answer Posted / venkat1435
main()
{
int n,count=0;
printf("enter a number");
scanf("%d",&n);//enterd no.is 5
while(n>0)
{
count++;
n=n&n-1;//binary of n is 101
// binary of n-1 is 100
//n&n-1 is (i.e 101
&100 =100 )
}
printf("%d",count);
getch();
} output is 2(i.e 2 ones in 101)
Is This Answer Correct ? | 31 Yes | 6 No |
Post New Answer View All Answers
What is a buffer in c?
What is the difference between c &c++?
Differentiate fundamental data types and derived data types in C.
What are the modifiers available in c programming language?
What does typeof return in c?
What is self-referential structure in c programming?
Explain c preprocessor?
What is typeof in c?
What is the use of clrscr?
How many types of operators are there in c?
What is main () in c language?
How are strings stored in c?
Can we change the value of static variable in c?
What is keyword in c?
Explain what math functions are available for integers? For floating point?