# Set the rightmost unset bit

Given a non-negative number n. The problem is to set the rightmost unset bit in the binary representation of n. If there are no unset bits, then just leave the number as it is.

Examples:

```Input : 21
Output : 23
(21)10 = (10101)2
Rightmost unset bit is at position 2(from right) as
highlighted in the binary representation of 21.
(23)10 = (10111)2
The bit at position 2 has been set.

Input : 15
Output : 15
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Following are the steps:

1. If n = 0, return 1.
2. If all bits of n are set, return n. Refer this post.
3. Else perform bitwise not on the given number(operation equivalent to 1’s complement). Let it be num = ~n.
4. Get the position of rightmost set bit of num. Let the position be pos.
5. Return (1 << (pos – 1)) | n.

## C++

 `// C++ implementation to set the rightmost unset bit ` `#include ` `using` `namespace` `std; ` ` `  `// function to find the position  ` `// of rightmost set bit ` `int` `getPosOfRightmostSetBit(``int` `n) ` `{ ` `    ``return` `log2(n&-n)+1; ` `} ` ` `  `int` `setRightmostUnsetBit(``int` `n) ` `{ ` `    ``// if n = 0, return 1 ` `    ``if` `(n == 0) ` `        ``return` `1; ` `     `  `    ``// if all bits of 'n' are set ` `    ``if` `((n & (n + 1)) == 0)     ` `        ``return` `n; ` `     `  `    ``// position of rightmost unset bit in 'n' ` `    ``// passing ~n as argument ` `    ``int` `pos = getPosOfRightmostSetBit(~n);     ` `     `  `    ``// set the bit at position 'pos' ` `    ``return` `((1 << (pos - 1)) | n); ` `} ` ` `  `// Driver program to test above ` `int` `main() ` `{ ` `    ``int` `n = 21; ` `    ``cout << setRightmostUnsetBit(n); ` `    ``return` `0; ` `}  `

## Java

 `// Java implementation to set  ` `// the rightmost unset bit ` ` `  `class` `GFG { ` `     `  `// function to find the position ` `// of rightmost set bit ` `static` `int` `getPosOfRightmostSetBit(``int` `n)  ` `{ ` `    ``return` `(``int``)((Math.log10(n & -n)) / (Math.log10(``2``))) + ``1``; ` `} ` ` `  `static` `int` `setRightmostUnsetBit(``int` `n)  ` `{ ` `    ``// if n = 0, return 1 ` `    ``if` `(n == ``0``) ` `    ``return` `1``; ` ` `  `    ``// if all bits of 'n' are set ` `    ``if` `((n & (n + ``1``)) == ``0``) ` `    ``return` `n; ` ` `  `    ``// position of rightmost unset bit in 'n' ` `    ``// passing ~n as argument ` `    ``int` `pos = getPosOfRightmostSetBit(~n); ` ` `  `    ``// set the bit at position 'pos' ` `    ``return` `((``1` `<< (pos - ``1``)) | n); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String arg[]) { ` `    ``int` `n = ``21``; ` `    ``System.out.print(setRightmostUnsetBit(n)); ` `} ` `} ` ` `  `// This code is contributed by Anant Agarwal. `

## Python3

 `# Python implementation to ` `# set the rightmost unset bit ` `import` `math ` ` `  `# function to find the position  ` `# of rightmost set bit ` `def` `getPosOfRightmostSetBit(n): ` ` `  `    ``return` `int``(math.log2(n&``-``n)``+``1``) ` ` `  `  `  `def` `setRightmostUnsetBit(n): ` ` `  `    ``# if n = 0, return 1 ` `    ``if` `(n ``=``=` `0``): ` `        ``return` `1` `      `  `    ``# if all bits of 'n' are set ` `    ``if` `((n & (n ``+` `1``)) ``=``=` `0``):     ` `        ``return` `n ` `      `  `    ``# position of rightmost unset bit in 'n' ` `    ``# passing ~n as argument ` `    ``pos ``=` `getPosOfRightmostSetBit(~n)     ` `      `  `    ``# set the bit at position 'pos' ` `    ``return` `((``1` `<< (pos ``-` `1``)) | n) ` ` `  `# Driver code ` ` `  `n ``=` `21` `print``(setRightmostUnsetBit(n)) ` ` `  `# This code is contributed ` `# by Anant Agarwal. `

Output:

```23
```

Alternate Implementation in Java
The idea is to use Integer.toBinaryString()

 `import` `java.io.*; ` `import` `java.util.Scanner; ` ` `  `public` `class` `SetMostRightUnsetBit { ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``int` `a = ``21``; ` `        ``setMostRightUnset(a); ` `    ``} ` ` `  `    ``private` `static` `void` `setMostRightUnset(``int` `a) ` `    ``{ ` `        ``// will get a number with all set bits except the  ` `        ``// first set bit ` `        ``int` `x = a ^ (a - ``1``); ` `        ``System.out.println(Integer.toBinaryString(x)); ` ` `  `        ``// We reduce it to the number with single 1's on  ` `        ``// the position of first set bit in given number ` `        ``x = x & a;  ` `        ``System.out.println(Integer.toBinaryString(x)); ` ` `  `        ``// Move x on right by one shift to make OR  ` `        ``// operation and make first rightest unset bit 1 ` `        ``x = x >> ``1``;  ` ` `  `        ``int` `b = a | x; ` ` `  `        ``System.out.println(``"before setiing bit "` `+  ` `                               ``Integer.toBinaryString(a)); ` `        ``System.out.println(``"after setting bit "` `+  ` `                               ``Integer.toBinaryString(b)); ` `    ``} ` `} `

This article is contributed by Ayush Jauhari. 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.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Improved By : JayPatel2

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.