Suppose we have a number n. We have to check whether the number has only two set bits at first and last position or not.
So, if the input is like n = 17, then the output will be True as the binary representation of n is 10001, there is only two 1's at first and last position.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
def is_pow_of_two(n): return (n & n-1) == 0 def solve(n): if n == 1: return True return is_pow_of_two (n-1) n = 17 print(solve(n))