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


Please Help Members By Posting Answers For Below Questions

number of times a digit is present in a number

1534


write a c program to do the following: a) To find the area of a triangle. b) To convert the temperature from Fahrenheit to Celsius. c) To convert the time in hours : minutes : seconds to seconds.

1512


What oops means?

570


A character flag or control mechanism that delineates one data item from another a) variable b) constant c) delimiter d) call by reference

622


What is oops c?

598






how to print electricity bill according to following charges first 100 units -1rs per unit for next 200 units-1.50 rs per unit without using conditions

2713


What is a static function in c?

610


A collection of data with a given structure for excepting storing and providing on demand data for multiple users a) linked list b) datastructer c) database d) preprocessor

616


What is break statement?

619


Why we not create function inside function.

1740


Is c high or low level?

569


Write a program to identify if a given binary tree is balanced or not.

673


Explain what is the difference between text files and binary files?

606


a=10;b= 5;c=3;d=3; if(a printf(%d %d %d %d a,b,c,d) else printf("%d %d %d %d a,b,c,d);

638


how is the examination pattern?

1587