Check whether the two numbers differ at one bit position only | function to check if x is power of 2 ; First x in the below expression is for the case when x is 0 ; function to check whether the two numbers differ at one bit position only ; Driver code to test above
def isPowerOfTwo ( x ) : NEW_LINE INDENT return x and ( not ( x & ( x - 1 ) ) ) NEW_LINE DEDENT def differAtOneBitPos ( a , b ) : NEW_LINE INDENT return isPowerOfTwo ( a ^ b ) NEW_LINE DEDENT a = 13 NEW_LINE b = 9 NEW_LINE if ( differAtOneBitPos ( a , b ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT