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 ios :: app do in c++?
What are keywords in c++?
Can you pass a vector to a function?
How many characters are recognized by ANSI C++?
What c++ is used for?
Explain all the C++ concepts using examples.
what is Loop function? What are different types of Loops?
Explain the uses oof nested class?
Why is c++ considered difficult?
Out of fgets() and gets() which function is safe to use and why?
Is c++ a high level language?
When there is a global variable and local variable with the same name, how will you access the global variable?
What is the first name of c++?
What is the basic structure of c++ program?
What are the advantages of inheritance in c++?