Maximize given integer by swapping pairs of unequal bits | Function to return the maximum possible value that can be obtained from the given integer by performing given operations ; Convert to equivalent binary representation ; Stores binary representation of the maximized value ; Store the count of 0 ' s ▁ and ▁ 1' s ; Stores the total Number of Bits ; If current bit is set ; Increment Count1 ; Increment Count0 ; Shift all set bits to left to maximize the value ; Shift all unset bits to right to maximize the value ; Return the maximized value ; Driver Code ; Given "Input ; Function call to find the Maximum Possible Number
def findMaxNum ( num ) : NEW_LINE INDENT binaryNumber = bin ( num ) [ 2 : ] NEW_LINE maxBinaryNumber = " " NEW_LINE count0 , count1 = 0 , 0 NEW_LINE N = len ( binaryNumber ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( binaryNumber [ i ] == '1' ) : NEW_LINE INDENT count1 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count0 += 1 NEW_LINE DEDENT DEDENT for i in range ( count1 ) : NEW_LINE INDENT maxBinaryNumber += '1' NEW_LINE DEDENT for i in range ( count0 ) : NEW_LINE INDENT maxBinaryNumber += '0' NEW_LINE DEDENT return int ( maxBinaryNumber , 2 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 11 NEW_LINE print ( findMaxNum ( N ) ) NEW_LINE DEDENT