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
What is public, protected, private in c++?
What is a class template?
How can you quickly find the number of elements stored in a static array?
What is the difference between global int and static int declaration?
Explain dangling pointer.
What is long in c++?
Explain register storage specifier.
Define friend function.
Explain polymorphism?
Define vptr.
What are the uses of static class data?
Should you pass exceptions by value or by reference?
Can we declare a base-class destructor as virtual?
What is an operator in c++?
Can we change the basic meaning of an operator in c++?