@@ -0,0 +1,999 @@
+#!/usr/bin/env python%0A%0A%22%22%22%0A%0ABrian Kernighan's method goes through as many iterations as there are set %0Abits. So if we have a 32-bit word with only the high bit set, then it will %0Aonly go once through the loop.%0A%0APublished in 1988, the C Programming Language 2nd Ed. (by Brian W. Kernighan %0Aand Dennis M. Ritchie) mentions this in exercise 2-9. On April 19, 2006 Don Knuth %0Apointed out to him that this method %22was first published by Peter Wegner in CACM 3 (1960), 322. %0A(Also discovered independently by Derrick Lehmer and published in 1964 in a book edited by Beckenbach.)%22%0A%0Along count_bits(long n) %7B     %0A  unsigned int c; // c accumulates the total bits set in v%0A  for (c = 0; n; c++) %0A    n &= n - 1; // clear the least significant bit set%0A  return c;%0A%7D%0A%0AShamelessly stolen an implemented in python%0A%0A%22%22%22%0Aimport os%0Aimport sys%0A%0Adef cnt_bits(v):%0A   c = 0%0A   n = int(v,0)%0A   while (n):%0A      n &= (n - 1)%0A      c += 1%0A   return c%0A%0Aif __name__ == '__main__':%0A   print cnt_bits(sys.argv%5B1%5D)%0A   %0A      %0A

Create countbits.py