Find the maximum between N and the number formed by reversing 32 | Function that obtains the number using said operations from N ; Stores the binary representation of the number N ; Find the binary representation of the number N ; Check for the set bits ; Reverse the string S ; Stores the obtained number ; Calculating the decimal value ; Check for set bits ; Function to find the maximum value between N and the obtained number ; Driver Code
def reverseBin ( N ) : NEW_LINE INDENT S = " " NEW_LINE i = 0 NEW_LINE for i in range ( 32 ) : NEW_LINE INDENT if ( N & ( 1 << i ) ) : NEW_LINE INDENT S += '1' NEW_LINE DEDENT else : NEW_LINE INDENT S += '0' NEW_LINE DEDENT DEDENT S = list ( S ) NEW_LINE S = S [ : : - 1 ] NEW_LINE S = ' ' . join ( S ) NEW_LINE M = 0 NEW_LINE for i in range ( 32 ) : NEW_LINE INDENT if ( S [ i ] == '1' ) : NEW_LINE INDENT M += ( 1 << i ) NEW_LINE DEDENT DEDENT return M NEW_LINE DEDENT def maximumOfTwo ( N ) : NEW_LINE INDENT M = reverseBin ( N ) NEW_LINE return max ( N , M ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 6 NEW_LINE print ( maximumOfTwo ( N ) ) NEW_LINE DEDENT