Archived

counting bits WIP

An old note — I haven't updated it since I wrote it.
1    def countBits(self, num):
2        
3        setBits = [0] * (num+1)
4        # (i & (i -1)) is actually Brian Kernighans Algorithm, so always keep it handy for counting ones
5        for i in range(1 ,num+1):
6            setBits[i] = setBits[i & (i-1)] + 1
7        return setBits