title: How can I check Hamming Weight without converting to binary?
body: <p>How can I get the <strong>number of "1"s</strong> in the binary representation of a number without actually converting and counting ?</p>
  
  <p>e.g.</p>
  
  <pre><code>  def number_of_ones(n):
        # do something
        # I want to MAKE this FASTER (computationally less complex).
        c = 0
        while n:
          c += n%2
          n /= 2
        return c
  
  
  &gt;&gt;&gt; number_of_ones(5)
      2
  &gt;&gt;&gt; number_of_ones(4)
      1
  </code></pre>