Find the maximum between N and the number formed by reversing 32 | Java program for the 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
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static int reverseBin ( int N ) { String S = " " ; int i ; for ( i = 0 ; i < 32 ; i ++ ) { if ( ( N & ( 1L << i ) ) != 0 ) S += '1' ; else S += '0' ; } S = ( new StringBuilder ( S ) ) . reverse ( ) . toString ( ) ; int M = 0 ; for ( i = 0 ; i < 32 ; i ++ ) { if ( S . charAt ( i ) == '1' ) M += ( 1L << i ) ; } return M ; } static int maximumOfTwo ( int N ) { int M = reverseBin ( N ) ; return Math . max ( N , M ) ; } public static void main ( String [ ] args ) { int N = 6 ; System . out . print ( maximumOfTwo ( N ) ) ; } }