def bits_set(n): i = 0 while n: if n & 1: yield i i += 1 n >>= 1