Given a non-negative number n. The problem is to set the rightmost unset bit in the binary representation of n

**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 : 2 Output : 3

One method is discussed in this article

This post discusses another method.

Let the input number be n. n+1 would have all the bits flipped after the rightmost unset bit (including the unset bit). So, doing n|(n+1) would give us the required result.

## C++

`#include <stdio.h> ` ` ` `// sets the rightmost unset bit ` `// of n and returns the result ` `int` `fun(unsigned ` `int` `n) ` `{ ` ` ` `return` `n | (n + 1); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 5; ` ` ` `printf` `(` `"The number after setting the"` `); ` ` ` `printf` `(` `" rightmost unset bit %d"` `, fun(n)); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`class` `GFG { ` ` ` ` ` `// sets the rightmost unset bit ` ` ` `// of n and returns the result ` ` ` `static` `int` `fun(` `int` `n) ` ` ` `{ ` ` ` `return` `n | (n + ` `1` `); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `5` `; ` ` ` `System.out.printf(` `"The number "` ` ` `+ ` `"after setting the"` `); ` ` ` `System.out.printf(` `" rightmost "` ` ` `+ ` `" unset bit %d"` `, fun(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Smitha ` |

*chevron_right*

*filter_none*

## Python3

`# sets the rightmost unset bit ` `# of n and returns the result ` `def` `fun(n): ` ` ` ` ` `return` `n | (n ` `+` `1` `) ` ` ` `# Driver Code ` `n ` `=` `5` `print` `(` `"The number after setting the"` `, end` `=` `"") ` `print` `(` `" rightmost unset bit "` `, fun(n)) ` ` ` `# This code is contributed by Smitha ` |

*chevron_right*

*filter_none*

## C#

`using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// sets the rightmost unset bit ` ` ` `// of n and returns the result ` ` ` `static` `int` `fun(` `int` `n) ` ` ` `{ ` ` ` `return` `n | (n + 1); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 5; ` ` ` `Console.Write(` `"The number "` ` ` `+ ` `"after setting the"` `); ` ` ` `Console.Write(` `" rightmost "` ` ` `+ ` `"unset bit "` `+ fun(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Smitha ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to Set the rightmost ` `// unset bit ` ` ` `// sets the rightmost unset bit ` `// of n and returns the result ` `function` `fun(` `$n` `) ` `{ ` ` ` `return` `$n` `| (` `$n` `+ 1); ` `} ` ` ` ` ` `// Driver Code ` ` ` `$n` `= 5; ` ` ` `echo` `"The number after setting the"` `; ` ` ` `echo` `" rightmost unset bit "` `, fun(` `$n` `); ` ` ` `// This code is contributed m_kit ` `?> ` |

*chevron_right*

*filter_none*

Output :

The number after setting the rightmost unset bit 7

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.

## Recommended Posts:

- Set the rightmost unset bit
- Get the position of rightmost unset bit
- Set the rightmost off bit
- Turn off the rightmost set bit
- Position of rightmost set bit
- Turn off the rightmost set bit | Set 2
- Position of rightmost different bit
- Number formed by the rightmost set bit in N
- Unset the last m bits
- Set the Left most unset bit
- Position of rightmost bit with first carry in sum of two binary
- Position of rightmost common bit in two numbers
- Unset bits in the given range
- Check whether the bit at given position is set or unset
- Count unset bits in a range
- Check whether all the bits are unset in the given range or not
- Count unset bits of a number
- Check whether all the bits are unset in the given range
- Absolute difference between set and unset bit count in N
- Number formed by flipping all bits to the left of rightmost set bit

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.