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 functions syntax in c++?
What are the steps in the development cycle?
What is while loops?
What does iomanip mean in c++?
We all know that a const variable needs to be initialized at the time of declaration. Then how come the program given below runs properly even when we have not initialized p?
What does #define mean in c++?
Can java be faster than c++?
What is else if syntax?
What are containers in c++?
Comment on local and global scope of a variable.
Explain the differences between list x; & list x();.
What is the correct syntax for inheritance a) class aclass : public superclass b) class aclass inherit superclass c) class aclass <-superclass
What do you mean by const correctness?
Define a pointer to a data member of the type pointer to pointer?
What is a pointer with example?