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 is the protected keyword used for?

1118


Is c++ a difficult language?

1141


What are protected members in c++?

1146


What is the oldest programming language?

1022


Write bites in Turbo c++ Header ("Include") Files.

1276


What kind of problems can be solved by a namespace?

1105


what is VOID?

1075


What is capacity in vector in c++?

1024


How is computer programming useful in real life?

1075


Will rust take over c++?

1074


When do we use copy constructors?

1088


How can you link a c program with a c function?

1085


Differentiate between a pointer and a reference with respect to c++.

1257


How do you write a function that can reverse a linked-list?

1126


What is the latest c++ version?

1114