Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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


Please Help Members By Posting Answers For Below Questions

What does namespace mean in c++?

1142


what you know about c++?

1199


Which operator cannot be overloaded c++?

1060


Define copy constructor.

1179


Write a program that takes a 5 digit number and calculates 2 power that number and prints it.

2628


Explain virtual class and friend class.

1101


Difference between Abstraction and encapsulation in C++?

1209


What are the advantages of using friend classes?

1200


What is a terminating character in c++?

1444


How do you generate a random number in c++?

1151


What is difference between class and structure in c++?

1251


Do class declarations end with a semicolon? Do class method definitions?

1197


What is singleton class in c++?

1094


What is format for defining a structure?

1136


What is purpose of new operator?

1126