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

Answer Posted / artyom

// It's still O(n), maybe there are better ways.
int countBit(int num)
{
int count = 0;
while(num)
{
count += static_cast<int>(static_cast<bool>(mask &
0x1));
num >>= 1;
}
return count;
}

Is This Answer Correct ?    2 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Give example of a pure virtual function in c++?

589


What is time_t c++?

615


Write about c++ storage classes?

757


What is a singleton class c++?

546


How is c++ used in the real world?

575






Explain stack & heap objects?

636


Write a program in C++ for Fibonacci series

664


What is meant by const_cast?

653


What is the role of copy constructor in copying of thrown objects?

602


Name the debugging methods that are used to solve problems?

583


What is array in c++ example?

653


what is oops and list its features in c++?

552


Who was the creator of c++?

564


Explain rethrowing exceptions with an example?

610


What is a volatile variable in c++?

561