import itertools import math def is_power_of_two_log(n): if n <= 0: return False power = round(math.log(n, 2)) return 2 ** power == n def is_power_of_two_bitwise(n): count = 0 while n and count <= 1: if n & 0b1: count += 1 n >>= 1 return not count != 1 for n in itertools.count(): r1 = is_power_of_two_log(n) r2 = is_power_of_two_bitwise(n) if r1 != r2: print '%d is inconsistent' % n