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
Can you add pointers together? Why would you?
What is the size of enum in bytes?
Why can't I perform arithmetic on a void* pointer?
What are pointers? Why are they used?
What is volatile variable in c with example?
What is the difference between local variable and global variable in c?
Is a house a shell structure?
What is structure pointer in c?
How many types of operator or there in c?
How are portions of a program disabled in demo versions?
Give the rules for variable declaration?
Explain what is the difference between a string and an array?
What is nested structure with example?
struct screen_pos{ int row, col } ;move_right(cursor)struct screen_pos *cursor;{ cursor.col++; } /* This statementhas a syntax error */What is the correct statement a) cursor.col = cursor.col + 1; b) col.cursor++; c) *cursor.col++; d) pointer
What is the difference between class and object in c?