Find the maximum between N and the number formed by reversing 32 | C ++ program for tha above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int reverseBin ( int N ) { string S = " " ; int i ; for ( i = 0 ; i < 32 ; i ++ ) { if ( N & ( 1LL << i ) ) S += '1' ; else S += '0' ; } reverse ( S . begin ( ) , S . end ( ) ) ; int M = 0 ; for ( i = 0 ; i < 32 ; i ++ ) { if ( S [ i ] == '1' ) M += ( 1LL << i ) ; } return M ; } int maximumOfTwo ( int N ) { int M = reverseBin ( N ) ; return max ( N , M ) ; } int main ( ) { int N = 6 ; cout << maximumOfTwo ( N ) ; return 0 ; }