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 does namespace mean in c++?
what you know about c++?
Which operator cannot be overloaded c++?
Define copy constructor.
Write a program that takes a 5 digit number and calculates 2 power that number and prints it.
Explain virtual class and friend class.
Difference between Abstraction and encapsulation in C++?
What are the advantages of using friend classes?
What is a terminating character in c++?
How do you generate a random number in c++?
What is difference between class and structure in c++?
Do class declarations end with a semicolon? Do class method definitions?
What is singleton class in c++?
What is format for defining a structure?
What is purpose of new operator?