Geeks Talk

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.

Fastest algorithm to find the number of bits set in a number?

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?...

Go Back   Geeks Talk > Software Development > C and C++
Register Blogs FAQ Tag Cloud Calendar Mark Forums Read
  #1 (permalink)  
Old 02-11-2007
Junior Member
 
Join Date: Jan 2007
Location: India
Posts: 9
Thanks: 0
Thanked 3 Times in 3 Posts
chandan1008 is on a distinguished road
Fastest algorithm to find the number of bits set in a number?

Hi, what is the fastest algorithm to find the number of bits set in a number?
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-15-2007
Junior Member
 
Join Date: Jan 2007
Location: India
Posts: 9
Thanks: 0
Thanked 3 Times in 3 Posts
chandan1008 is on a distinguished road
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?
Reply With Quote
The Following User Says Thank You to chandan1008 For This Useful Post:
  #3 (permalink)  
Old 06-29-2007
Junior Member
 
Join Date: Jun 2007
Location: Delhi, India
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
sandeep6883 is on a distinguished road
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).
Reply With Quote
  #4 (permalink)  
Old 08-27-2008
Junior Member
 
Join Date: Aug 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
jurgenthor is on a distinguished road
Re: Fastest algorithm to find the number of bits set in a number?

Create a lookup table - O(1).
Reply With Quote
  #5 (permalink)  
Old 08-27-2008
Expert Member
 
Join Date: Apr 2008
Location: Tamilnadu
Posts: 1,465
Thanks: 15
Thanked 150 Times in 141 Posts
deepasree has a spectacular aura aboutdeepasree has a spectacular aura about
Re: Fastest algorithm to find the number of bits set in a number?

hi friend..

just refer the link

Bit Count « Technical Interview Questions

Thanks
Deepasree
Reply With Quote
Reply

  Geeks Talk > Software Development > C and C++

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


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


All times are GMT -4. The time now is 08:10 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.1
Copyright © 2009 GeekInterview.com. All Rights Reserved