-
Junior Member
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?
-
Junior Member
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?
-
Junior Member
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).
-
Junior Member
Re: Fastest algorithm to find the number of bits set in a number?
Create a lookup table - O(1).
-
Expert Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules