Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check Hamming Weight without converting to binary?

How can I get the number of "1"s in the binary representation of a number without actually converting and counting ?

e.g.

  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


>>> number_of_ones(5)
    2
>>> number_of_ones(4)
    1
like image 972
Pratik Deoghare Avatar asked May 09 '09 18:05

Pratik Deoghare


People also ask

How do you calculate hamming weight?

So, for an integer x, we first divide it into A and B as explained above, and then we lookup dp[A] and dp[B] to get the set bits in the integer x. We repeat the same process for all integers in the array s and keep a count of the total number of set bits, i.e., the hamming weight of the array.

How do you find the weight of a Hamming Python?

The easiest way to get the Hamming Weight (count of bits that are set for a number) is to convert the number to its binary representation as a string using the bin-function. It will return a string like 0b1101 for the number 13. Then all that is left to do is to count the ones.

How do you calculate hamming weight in Java?

Java Basic: Exercise-249 with Solution The Hamming weight of a string is the number of symbols that are different from the zero-symbol of the alphabet used. It is thus equivalent to the Hamming distance from the all-zero string of the same length.

What is hamming weight of a code word?

The Hamming weight of a codeword C is defined as the number of non-zero elements (i.e., 1s) in the codeword. The Hamming distance between two codewords is defined as the number of elements in which they differ.


2 Answers

I'm not a python programmer, but hopefully it will be enough for you to follow.

c = 0
while n:
    c += 1
    n &= n - 1

return c

While a little obscure, it's primary advantage is speed and simplicity. The while loop is only iterated once for every bit set to 1 in n.

like image 179
Stephen Nutt Avatar answered Oct 23 '22 04:10

Stephen Nutt


You cannot make this computationally less complex. It will be O(n) the number of bits, or, as the answer with the & trick showed, O(n) the number of bits set to 1; but unless all of the numbers you are using are a special case, the latter should on average be n/2, so both of those O(n) numbers are the same.

And the lookup-table trick, of course, is actually doing nothing for the computational complexity; it's just paying for time with space but without changing the underlying economics, which are that you must examine each bit once somehow and there is no way around that. You cannot, logically, answer a question about the bits in the number without inspecting each of them.

Now, I suppose I'm being a bit sloppy since many of these examples are actually O(n^2) since in Python you have to examine the whole number at once time, so with a Python long integer of, say, 100 bytes, a + or an & or a / operation will look at each byte at least once, and that will happen over and over until the number is reduced to zero (in the schemes outlined above), so these, again, are really O(n^2) operations. I am not sure Python will allow a true O(n) solution here.

Anyway: if you were really asking about computational complexity, which specifically means big-O analysis, that's your answer. :-)

like image 43
Brandon Rhodes Avatar answered Oct 23 '22 06:10

Brandon Rhodes