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
GIVEN A FLOATING POINT NUMBER HOW IS IT ACTUALLY STORED IN MEMORY ? CAN ANYONE EXPLAIN?? THE 32 BIT REPRESENTATION OF A FLOATING POINT NUMBER ALLOTS: 1 BIT-SIGN 8 BITS-EXPONENT 23 BITS-MANTISSA
What is static memory allocation?
Is python a c language?
Why is c still so popular?
What does the file stdio.h contain?
a way in which a pointer stores the address of a pointer which stores the value of the target value a) reference b) allocation c) multiple indirection d) none
The file stdio.h, what does it contain?
What is const volatile variable in c?
Can we declare function inside main?
How are pointers declared in c?
To print the pattern 1 2 3 4 5 10 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9
why wipro wase
What are different types of variables in c?
What is meant by type specifiers?
What is the difference between #include and #include 'file' ?