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 union and structure in c?
Are pointers integers in c?
What is the right type to use for boolean values in c? Is there a standard type? Should I use #defines or enums for the true and false values?
Given an array of 1s and 0s arrange the 1s together and 0s together in a single scan of the array. Optimize the boundary conditions?
What does malloc () calloc () realloc () free () do?
What does void main return?
What do you mean by dynamic memory allocation in c? What functions are used?
Why c is called top down?
write a program that types this pattern: 12345678987654321 12345678 87654321 1234567 7654321 123456 654321 12345 54321 1234 4321 123 321 12 21 1 1
What are c preprocessors?
Explain the use of #pragma exit?
What is scope rule of function in c?
What is the difference between functions getch() and getche()?
Explain what is a pragma?
Differentiate between ordinary variable and pointer in c.