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 pre-emptive data structure and explain it with example?
What are the disadvantages of external storage class?
What are structure members?
Draw a diagram showing how the operating system relates to users, application programs, and the computer hardware ?
Why do we use return in c?
write a C program: To search a file any word which starts with ?a?. If the word following this ?a? starts with a vowel.Then replace this ?a? with ?a? with ?an?. redirect with the output onto an output file.The source file and destination file are specified by the user int the command line.
Why are all header files not declared in every c program?
What is Dynamic memory allocation in C? Name the dynamic allocation functions.
What are run-time errors?
Can a pointer point to null?
What is malloc return c?
Is there a way to jump out of a function or functions?
Explain what does the characters 'r' and 'w' mean when writing programs that will make use of files?
What is methods in c?
how is the examination pattern?