# Maximize a given unsigned number number by swapping bits at it’s extreme positions.

Given a number maximize it by swapping bits at it’s extreme positions i.e. at first and last position, second and second last position and so on.

**Examples:**

Input : 4 (0000...0100) Output : 536870912 (0010...0000) In the above example swapped 3rd and 3rd last bit to maximize the given unsigned number. Input : 12 (00000...1100) Output : 805306368 (0011000...0000) In the above example 3rd and 3rd last bit and 4th and 4th last bit are swapped to maximize the given unsigned number.

**Naive Approach: **

**1.** Convert the number into it’s bit representation and store it’s bit representation in an array.

**2.** Traverse the array from both ends, if the less significant bit of the bit representation is greater than the more significant bit i.e. if less significant bit is 1 and more significant bit is 0 then swap them else take no action.

**3.** Convert the obtained binary representation back to the number.

**Efficient Approach: **

**1.** Create a copy of the original number because the original number would be modified, iteratively obtain the bits at the extreme positions.

**2.** If less significant bit is 1 and more significant bit is 0 then swap the bits in the bit from only, continue the process until less significant bit’s position is less than more significant bit’s position.

**3.** Display the maximized number.

## C++

`// C++ program to find maximum number by ` `// swapping extreme bits. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `#define ull unsigned long long int ` ` ` `ull findMax(ull num) ` `{ ` ` ` `ull num_copy = num; ` ` ` ` ` `/* Traverse bits from both extremes */` ` ` `int` `j = ` `sizeof` `(unsigned ` `long` `long` `int` `) * 8 - 1; ` ` ` `int` `i = 0; ` ` ` `while` `(i < j) { ` ` ` ` ` `// Obtaining i-th and j-th bits ` ` ` `int` `m = (num_copy >> i) & 1; ` ` ` `int` `n = (num_copy >> j) & 1; ` ` ` ` ` `/* Swapping the bits if lesser significant ` ` ` `is greater than higher significant ` ` ` `bit and accordingly modifying the number */` ` ` `if` `(m > n) { ` ` ` `int` `x = (1 << i | 1 << j); ` ` ` `num = num ^ x; ` ` ` `} ` ` ` ` ` `i++; ` ` ` `j--; ` ` ` `} ` ` ` `return` `num; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `ull num = 4; ` ` ` `cout << findMax(num); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find maximum number by ` `// swapping extreme bits. ` ` ` `class` `GFG { ` ` ` ` ` `static` `int` `findMax(` `int` `num) { ` ` ` `byte` `size_of_int = ` `4` `; ` ` ` `int` `num_copy = num; ` ` ` ` ` `/* Traverse bits from both extremes */` ` ` `int` `j = size_of_int * ` `8` `- ` `1` `; ` ` ` `int` `i = ` `0` `; ` ` ` `while` `(i < j) { ` ` ` ` ` `// Obtaining i-th and j-th bits ` ` ` `int` `m = (num_copy >> i) & ` `1` `; ` ` ` `int` `n = (num_copy >> j) & ` `1` `; ` ` ` ` ` `/* Swapping the bits if lesser significant ` ` ` `is greater than higher significant ` ` ` `bit and accordingly modifying the number */` ` ` `if` `(m > n) { ` ` ` `int` `x = (` `1` `<< i | ` `1` `<< j); ` ` ` `num = num ^ x; ` ` ` `} ` ` ` ` ` `i++; ` ` ` `j--; ` ` ` `} ` ` ` `return` `num; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `public` `void` `main(String[] args) { ` ` ` `int` `num = ` `4` `; ` ` ` `System.out.println(findMax(num)); ` ` ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python 3

# Python 3 program to find maximum number

# by swapping extreme bits.

def findMax( num):

num_copy = num

# Traverse bits from both extremes

j = 4 * 8 – 1;

i = 0

while (i < j) :
# Obtaining i-th and j-th bits
m = (num_copy >> i) & 1

n = (num_copy >> j) & 1

# Swapping the bits if lesser significant

# is greater than higher significant

# bit and accordingly modifying the number

if (m > n) :

x = (1 << i | 1 << j)
num = num ^ x
i += 1
j -= 1
return num
# Driver code
if __name__ == "__main__":
num = 4
print(findMax(num))
# This code is contributed by ita_c
[tabby title="C#"]

` ` `// C# program to find maximum number by ` `// swapping extreme bits. ` `using` `System; ` `public` `class` `GFG { ` ` ` ` ` `static` `int` `findMax(` `int` `num) { ` ` ` `byte` `size_of_int = 4; ` ` ` `int` `num_copy = num; ` ` ` ` ` `/* Traverse bits from both extremes */` ` ` `int` `j = size_of_int * 8 - 1; ` ` ` `int` `i = 0; ` ` ` `while` `(i < j) { ` ` ` ` ` `// Obtaining i-th and j-th bits ` ` ` `int` `m = (num_copy >> i) & 1; ` ` ` `int` `n = (num_copy >> j) & 1; ` ` ` ` ` `/* Swapping the bits if lesser significant ` ` ` `is greater than higher significant ` ` ` `bit and accordingly modifying the number */` ` ` `if` `(m > n) { ` ` ` `int` `x = (1 << i | 1 << j); ` ` ` `num = num ^ x; ` ` ` `} ` ` ` ` ` `i++; ` ` ` `j--; ` ` ` `} ` ` ` `return` `num; ` ` ` `} ` ` ` `// Driver code ` ` ` `static` `public` `void` `Main() { ` ` ` `int` `num = 4; ` ` ` `Console.Write(findMax(num)); ` ` ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

536870912

This article is contributed by **Aditya Gupta**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Maximize the number by rearranging bits
- Minimum flips required to maximize a number with k set bits
- Check if bits of a number has count of consecutive set bits in increasing order
- Check if a number has same number of set and unset bits
- Number of 0s and 1s at prime positions in the given array
- Toggle bits of a number except first and last bits
- Minimum number using set bits of a given number
- Number of integers with odd number of set bits
- Next higher number with same number of set bits
- M-th smallest number having k number of set bits.
- Set all even bits of a number
- Same Number Of Set Bits As N
- Set all odd bits of a number
- Largest number less than X having at most K set bits
- Number with set bits only between L-th and R-th index