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(): for j in [-1, 0, 1]: r1 = is_power_of_two_log((2 ** n) + j) r2 = is_power_of_two_bitwise((2 ** n) + j) if r1 != r2: print '%d is inconsistent' % n