Results 1 to 5 of 5

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

  1. #1
    Junior Member
    Join Date
    Jan 2007
    Answers
    9

    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?


  2. #2
    Junior Member
    Join Date
    Jan 2007
    Answers
    9

    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?


  3. #3
    Junior Member
    Join Date
    Jun 2007
    Answers
    4

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


  4. #4
    Junior Member
    Join Date
    Aug 2008
    Answers
    1

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

    Create a lookup table - O(1).


  5. #5
    Expert Member
    Join Date
    Apr 2008
    Answers
    1,859

    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


  6.  Sponsored Ads
    Ad


     

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact