Count number of 1's in binary representation

后端 未结 21 1931
天涯浪人
天涯浪人 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 01:54

    I saw the following solution from another website:

    int count_one(int x){
        x = (x & (0x55555555)) + ((x >> 1) & (0x55555555));
        x = (x & (0x33333333)) + ((x >> 2) & (0x33333333));
        x = (x & (0x0f0f0f0f)) + ((x >> 4) & (0x0f0f0f0f));
        x = (x & (0x00ff00ff)) + ((x >> 8) & (0x00ff00ff));
        x = (x & (0x0000ffff)) + ((x >> 16) & (0x0000ffff));
        return x;
    }
    
    0 讨论(0)
  • 2020-11-28 01:54
       countBits(x){
         y=0;
         while(x){   
           y += x &  1 ;
           x  = x >> 1 ;
         }
       }
    

    thats it?

    0 讨论(0)
  • 2020-11-28 01:55

    I have actually done this using a bit of sleight of hand: a single lookup table with 16 entries will suffice and all you have to do is break the binary rep into nibbles (4-bit tuples). The complexity is in fact O(1) and I wrote a C++ template which was specialized on the size of the integer you wanted (in # bits)… makes it a constant expression instead of indetermined.

    fwiw you can use the fact that (i & -i) will return you the LS one-bit and simply loop, stripping off the lsbit each time, until the integer is zero — but that’s an old parity trick.

    0 讨论(0)
  • 2020-11-28 01:56

    The following is a C solution using bit operators:

    int numberOfOneBitsInInteger(int input) {
      int numOneBits = 0;
    
      int currNum = input;
      while (currNum != 0) {
        if ((currNum & 1) == 1) {
          numOneBits++;
        }
        currNum = currNum >> 1;
      }
      return numOneBits;
    }
    

    The following is a Java solution using powers of 2:

    public static int numOnesInBinary(int n) {
    
      if (n < 0) return -1;
    
      int j = 0;
      while ( n > Math.pow(2, j)) j++;
    
      int result = 0;
      for (int i=j; i >=0; i--){
        if (n >= Math.pow(2, i)) {
            n = (int) (n - Math.pow(2,i));
            result++;    
        }
      }
    
      return result;
    }
    
    0 讨论(0)
  • 2020-11-28 01:58

    Ruby implementation

    def find_consecutive_1(n)
      num = n.to_s(2)
      arr = num.split("")
      counter = 0
      max = 0
      arr.each do |x|
          if x.to_i==1
              counter +=1
          else
              max = counter if counter > max
              counter = 0 
          end
          max = counter if counter > max  
      end
      max
    end
    
    puts find_consecutive_1(439)
    
    0 讨论(0)
  • 2020-11-28 01:59

    Below are two simple examples (in C++) among many by which you can do this.

    1. We can simply count set bits (1's) using __builtin_popcount().

      int numOfOnes(int x) { return __builtin_popcount(x); }

    2. Loop through all bits in an integer, check if a bit is set and if it is then increment the count variable.

      int hammingDistance(int x) { int count = 0 for(int i = 0; i < 32; i++) if(x & (1 << i)) count++; return count; }

    Hope this helps!

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