What is the most efficient way to count the number of bits
which are set in a value?

Answer Posted / pappu kumar sharma

int fnCntbts(int num )
{
int iCnt = 0;
while ( num )
{
num &= (num-1) ;
iCnt++;
}

return iCnt;

}

Is This Answer Correct ?    11 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the basic structure of c?

797


Can stdout be forced to print somewhere other than the screen?

868


What is the Purpose of 'extern' keyword in a function declaration?

865


What is static memory allocation? Explain

834


What is a scope resolution operator in c?

974


What is this infamous null pointer, anyway?

805


what is a function method?give example?

2140


If fflush wont work, what can I use to flush input?

863


A c program to display count values from 0 to 100 and flash each digit for a secong.reset the counter after it reaches 100.use for loop,. pls guys hepl me.. :(

2016


an expression contains relational operators, assignment operators, and arithmatic operstors. In the absence of parentheses, they will be evaluated in which of the following order a) assignment, relational, arithematic b) arithematic, relational, assignment c) relational, arithematic, assignment d) assignment, arithematic, relational

1096


How can I recover the file name given an open stream or file descriptor?

853


What are the advantages of using linked list for tree construction?

867


What is #ifdef ? What is its application?

868


Take an MxN matrice from user and then sum upper diagonal in a variable and lower diagonal in a separate variables. Print the result

1706


the real constant in c can be expressed in which of the following forms a) fractional form only b) exponential form only c) ascii form only d) both a and b

2396