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 ios :: app do in c++?

966


What are keywords in c++?

1049


Can you pass a vector to a function?

912


How many characters are recognized by ANSI C++?

1361


What c++ is used for?

1012


Explain all the C++ concepts using examples.

1155


what is Loop function? What are different types of Loops?

1165


Explain the uses oof nested class?

1096


Why is c++ considered difficult?

1096


Out of fgets() and gets() which function is safe to use and why?

1135


Is c++ a high level language?

947


When there is a global variable and local variable with the same name, how will you access the global variable?

1048


What is the first name of c++?

1011


What is the basic structure of c++ program?

1036


What are the advantages of inheritance in c++?

1048