Give a very good method to count the number of ones in a "n"
(e.g. 32) bit number.

Answer Posted / manish kumar

for (c = 0; n; c++)
n &= n - 1;

Result: the value of c.

complexity:o(logn)

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can I create my own functions in c++?

575


What is static class data?

554


What is type of 'this' pointer?

574


which one is equivalent to multiplying by 2:Left shifting a number by 1 or Left shifting an unsigned int or char by 1?

691


What is a container class?

594






What is conditions when using boolean operators?

581


What is c++ and its uses?

598


Is it possible to get the source code back from binary file?

687


Can you sort a set c++?

504


what kind of projects are suitable for c and c++

597


What is stack unwinding?

579


Assume an array of structure is in order by studentID field of the record, where student IDs go from 101 to 500. Write the most efficient pseudocode algorithm you can to find the record with a specific studentID if every single student ID from 101 to 500 is used and the array has 400 elements. Write the most efficient pseudocode algorithm you can to find a record with a studentID near the end of the IDs, say in the range from 450 to 500, if not every single student ID in the range of 101 to 500 is used and the array size is only 300

1761


How do we balance an AVL Tree in C++?

607


List down the guideline that should be followed while using friend function.

626


What does h mean in maths?

545