human
Check if a number can be expressed as 2 ^ x + 2 ^ y | Utility function to check if a number is power of 2 or not.
Utility function to determine the value of previous power of 2.
function to check if n can be expressed as 2 ^ x + 2 ^ y or not.
if value of n is 0 or 1 it can not be expressed as 2 ^ x + 2 ^ y.
if n is power of two then it can be expressed as sum of 2 ^ x + 2 ^ y.
if the remainder after subtracting previous power of 2 is also a power of 2 then it can be expressed as 2 ^ x + 2 ^ y.
driver code"
How can the above be solved in Python?
assistant
#Here is code to check if a number can be expressed as 2 ^ x + 2 ^ y and the other steps required.
 def isPowerOfTwo ( n ) :
	return ( n and ( not ( n & ( n - 1 ) ) ) )
def previousPowerOfTwo ( n ) :
	while ( n & n - 1 ) :
		n = n & n - 1
	return n
def checkSum ( n ) :
	if ( n == 0 or n == 1 ) :
		return False
	elif ( isPowerOfTwo ( n ) ) :
		print ( n // 2 , n // 2 )
		return True
	else :
		x = previousPowerOfTwo ( n )
		y = n - x ;
		if ( isPowerOfTwo ( y ) ) :
			print ( x , y )
			return True
		else :
			return False
n1 = 20
if ( checkSum ( n1 ) ) :
	print ( " No " )
n2 = 11
if ( checkSum ( n2 ) ) :
	print ( " No " )