Related Articles

# Find the maximum between N and the number formed by reversing 32-bit binary representation of N

• Last Updated : 28 Jun, 2021

Given a positive 32-bit integer N, the task is to find the maximum between the value of N and the number obtained by decimal representation of reversal of binary representation of N in a 32-bit integer.

Examples:

Input: N = 6
Output: 1610612736
Explanation:
Binary representation of 6 in a 32-bit integer is 00000000000000000000000000000110 i.e., (00000000000000000000000000000110)2 = (6)10.
Reversing this binary string gives (01100000000000000000000000000000)2 = (1610612736)10.
The maximum between N and the obtained number is 1610612736. Therefore, print 1610612736.

Input: N = 1610612736
Output: 1610612736

Approach: Follow the steps below to solve the problem:

Below is the implementation of the above approach:

## C++

 `// C++ program for tha above approach` `#include ``using` `namespace` `std;` `// Function that obtains the number``// using said operations from N``int` `reverseBin(``int` `N)``{``    ``// Stores the binary representation``    ``// of the number N``    ``string S = ``""``;``    ``int` `i;` `    ``// Find the binary representation``    ``// of the number N``    ``for` `(i = 0; i < 32; i++) {` `        ``// Check for the set bits``        ``if` `(N & (1LL << i))``            ``S += ``'1'``;``        ``else``            ``S += ``'0'``;``    ``}` `    ``// Reverse the string S``    ``reverse(S.begin(), S.end());` `    ``// Stores the obtained number``    ``int` `M = 0;` `    ``// Calculating the decimal value``    ``for` `(i = 0; i < 32; i++) {` `        ``// Check for set bits``        ``if` `(S[i] == ``'1'``)``            ``M += (1LL << i);``    ``}``    ``return` `M;``}` `// Function to find the maximum value``// between N and the obtained number``int` `maximumOfTwo(``int` `N)``{``    ``int` `M = reverseBin(N);` `    ``return` `max(N, M);``}` `// Driver Code``int` `main()``{``    ``int` `N = 6;``    ``cout << maximumOfTwo(N);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.io.*;``import` `java.lang.*;``import` `java.util.*;` `public` `class` `GFG {` `    ``// Function that obtains the number``    ``// using said operations from N``    ``static` `int` `reverseBin(``int` `N)``    ``{``        ``// Stores the binary representation``        ``// of the number N``        ``String S = ``""``;``        ``int` `i;` `        ``// Find the binary representation``        ``// of the number N``        ``for` `(i = ``0``; i < ``32``; i++) {` `            ``// Check for the set bits``            ``if` `((N & (1L << i)) != ``0``)``                ``S += ``'1'``;``            ``else``                ``S += ``'0'``;``        ``}` `        ``// Reverse the string S``        ``S = (``new` `StringBuilder(S)).reverse().toString();` `        ``// Stores the obtained number``        ``int` `M = ``0``;` `        ``// Calculating the decimal value``        ``for` `(i = ``0``; i < ``32``; i++) {` `            ``// Check for set bits``            ``if` `(S.charAt(i) == ``'1'``)``                ``M += (1L << i);``        ``}``        ``return` `M;``    ``}` `    ``// Function to find the maximum value``    ``// between N and the obtained number``    ``static` `int` `maximumOfTwo(``int` `N)``    ``{``        ``int` `M = reverseBin(N);` `        ``return` `Math.max(N, M);``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{` `        ``int` `N = ``6``;``        ``System.out.print(maximumOfTwo(N));``    ``}``}` `// This code is conntributed by Kingash.`

## Python3

 `# Python3 program for tha above approach` `# Function that obtains the number``# using said operations from N``def` `reverseBin(N):``    ` `    ``# Stores the binary representation``    ``# of the number N``    ``S ``=` `""``    ``i ``=` `0` `    ``# Find the binary representation``    ``# of the number N``    ``for` `i ``in` `range``(``32``):``        ` `        ``# Check for the set bits``        ``if` `(N & (``1` `<< i)):``            ``S ``+``=` `'1'``        ``else``:``            ``S ``+``=` `'0'` `    ``# Reverse the string S``    ``S ``=` `list``(S)``    ``S ``=` `S[::``-``1``]``    ``S ``=` `''.join(S)``    ` `    ``# Stores the obtained number``    ``M ``=` `0` `    ``# Calculating the decimal value``    ``for` `i ``in` `range``(``32``):``        ` `        ``# Check for set bits``        ``if` `(S[i] ``=``=` `'1'``):``            ``M ``+``=` `(``1` `<< i)``            ` `    ``return` `M` `# Function to find the maximum value``# between N and the obtained number``def` `maximumOfTwo(N):``    ` `    ``M ``=` `reverseBin(N)` `    ``return` `max``(N, M)` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``N ``=` `6``    ``print``(maximumOfTwo(N))` `# This code is contributed by SURENDRA_GANGWAR`

## C#

 `// C# program for tha above approach``using` `System;` `class` `GFG{``static` `string` `ReverseString(``string` `s)``    ``{``        ``char``[] array = s.ToCharArray();``        ``Array.Reverse(array);``        ``return` `new` `string``(array);``    ``}``  ` `// Function that obtains the number``// using said operations from N``public` `static` `int` `reverseBin(``int` `N)``{``  ` `    ``// Stores the binary representation``    ``// of the number N``    ``string` `S = ``""``;``    ``int` `i;` `    ``// Find the binary representation``    ``// of the number N``    ``for` `(i = 0; i < 32; i++) {` `        ``// Check for the set bits``        ``if` `((N & (1L << i)) != 0)``            ``S += ``'1'``;``        ``else``            ``S += ``'0'``;``    ``}` `    ``// Reverse the string S``    ``S = ReverseString(S);``    `  `    ``// Stores the obtained number``    ``int` `M = 0;` `    ``// Calculating the decimal value``    ``for` `(i = 0; i < 32; i++) {` `        ``// Check for set bits``        ``if` `(S[i] == ``'1'``)``            ``M +=  (1 << i);``    ``}``    ``return` `M;``}` `// Function to find the maximum value``// between N and the obtained number``static` `int` `maximumOfTwo(``int` `N)``{``    ``int` `M = reverseBin(N);` `    ``return` `Math.Max(N, M);``}` `// Driver Code``static` `void` `Main()``{``    ``int` `N = 6;``    ``Console.Write(maximumOfTwo(N));` `}``}` `// This code is contributed by SoumikMondal`

## Javascript

 ``
Output:
`1610612736`

Time Complexity: O(32)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up