Count number of 1's in binary representation

后端 未结 21 1933
天涯浪人
天涯浪人 2020-11-28 01:32

Efficient way to count number of 1s in the binary representation of a number in O(1) if you have enough memory to play with. This is an interview question I found on an onli

相关标签:
21条回答
  • 2020-11-28 02:00
    public static void main(String[] args) {
    
        int a = 3;
        int orig = a;
        int count = 0;
        while(a>0)
        {
            a = a >> 1 << 1;
            if(orig-a==1)
                count++;
            orig = a >> 1;
            a = orig;
        }
    
        System.out.println("Number of 1s are: "+count);
    }
    
    0 讨论(0)
  • 2020-11-28 02:03

    In python or any other convert to bin string then split it with '0' to get rid of 0's then combine and get the length.

    len(''.join(str(bin(122011)).split('0')))-1
    
    0 讨论(0)
  • 2020-11-28 02:05

    Two ways::

    /* Method-1 */
    int count1s(long num)
    {
        int tempCount = 0;
    
        while(num)
        {
            tempCount += (num & 1); //inc, based on right most bit checked
            num = num >> 1;         //right shift bit by 1
        }
    
        return tempCount;
    }
    
    /* Method-2 */
    int count1s_(int num)
    {
        int tempCount = 0;
    
        std::string strNum = std::bitset< 16 >( num ).to_string(); // string conversion
        cout << "strNum=" << strNum << endl;
        for(int i=0; i<strNum.size(); i++)
        {
            if('1' == strNum[i])
            {
                tempCount++;
            }
        }
    
        return tempCount;
    }
    
    /* Method-3 (algorithmically - boost string split could be used) */
    1) split the binary string over '1'.
    2) count = vector (containing splits) size - 1
    

    Usage::

        int count = 0;
    
        count = count1s(0b00110011);
        cout << "count(0b00110011) = " << count << endl; //4
    
        count = count1s(0b01110110);
        cout << "count(0b01110110) = " << count << endl;  //5
    
        count = count1s(0b00000000);
        cout << "count(0b00000000) = " << count << endl;  //0
    
        count = count1s(0b11111111);
        cout << "count(0b11111111) = " << count << endl;  //8
    
        count = count1s_(0b1100);
        cout << "count(0b1100) = " << count << endl;  //2
    
        count = count1s_(0b11111111);
        cout << "count(0b11111111) = " << count << endl;  //8
    
        count = count1s_(0b0);
        cout << "count(0b0) = " << count << endl;  //0
    
        count = count1s_(0b1);
        cout << "count(0b1) = " << count << endl;  //1
    
    0 讨论(0)
  • 2020-11-28 02:06

    Below will work as well.

    nofone(int x) {
      a=0;
      while(x!=0) {
        x>>=1;
        if(x & 1)
          a++;
      }
      return a;
    } 
    
    0 讨论(0)
  • 2020-11-28 02:07

    I had to golf this in ruby and ended up with

    l=->x{x.to_s(2).count ?1}
    

    Usage :

    l[2**32-1] # returns 32

    Obviously not efficient but does the trick :)

    0 讨论(0)
  • 2020-11-28 02:08

    Please note the fact that: n&(n-1) always eliminates the least significant 1.

    Hence we can write the code for calculating the number of 1's as follows:

    count=0;
    while(n!=0){
      n = n&(n-1);
      count++;
    }
    cout<<"Number of 1's in n is: "<<count;
    

    The complexity of the program would be: number of 1's in n (which is constantly < 32).

    0 讨论(0)
提交回复
热议问题