Prepare for your Next Interview
|
Welcome to the Geeks Talk forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
This is a discussion on Fastest algorithm to find the number of bits set in a number? within the C and C++ forums, part of the Software Development category; Hi, what is the fastest algorithm to find the number of bits set in a number?...
|
|||||||
|
|||
|
Re: Fastest algorithm to find the number of bits set in a number?
for(count=0;number;count++)
number&=(number-1); can anyone write a better logic for this? |
| The Following User Says Thank You to chandan1008 For This Useful Post: | ||
|
|||
|
Re: Fastest algorithm to find the number of bits set in a number?
For a number 'n'
c=0; while(n) { c=c + (n&1); n=n>>1; } This is the best algorithm as for a number 'n' the time taken is O(log n). |
|
|||
|
Re: Fastest algorithm to find the number of bits set in a number?
|
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| find out the number. | jamesravid | Brainteasers | 7 | 01-25-2007 11:43 PM |
| Find the number | Manojks | Brainteasers | 16 | 01-20-2007 11:12 AM |
| find the number | vmshenoy | Brainteasers | 13 | 01-19-2007 06:30 AM |
| Find the number | Manojks | Brainteasers | 1 | 01-04-2007 07:58 AM |
| Can you find the number | SriramKrishna | Brainteasers | 4 | 10-25-2006 12:56 PM |