Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Bit hack solution in Speedy category for Binary Count by DiZ
def checkio(n):
'''Bit twiddling hack by Brian Kernighan.
This method goes through as many iterations as there are set bits.
So if we have a 32-bit word with only the high bit set,
then it will only go once through the loop.'''
c = 0
while n:
c += 1
n &= n-1
return c
March 10, 2014
Comments: