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 the protected keyword used for?
Is c++ a difficult language?
What are protected members in c++?
What is the oldest programming language?
Write bites in Turbo c++ Header ("Include") Files.
What kind of problems can be solved by a namespace?
what is VOID?
What is capacity in vector in c++?
How is computer programming useful in real life?
Will rust take over c++?
When do we use copy constructors?
How can you link a c program with a c function?
Differentiate between a pointer and a reference with respect to c++.
How do you write a function that can reverse a linked-list?
What is the latest c++ version?