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

What is a unnitialised pointer?

650


What does std :: flush do?

783


What does it mean to declare a destructor as static?

714


What is &x in c++?

665


What is an undefined reference/unresolved external symbol error and how do I fix it?

701






Define a constructor - what it is and how it might be called (2 methods)?

696


What is a syntax in c++?

718


Are iterators pointers?

773


Explain how the virtual base class is different from the conventional base classes of the opps.

794


What is the advantage of an external iterator.

655


How to tokenize a string in c++?

691


List the special characteristics of constructor.

832


What is a pointer with example?

756


What can I safely assume about the initial values of variables which are not explicitly initialized?

714


What is c++ good for?

683