# Turn off the rightmost set bit

Write a program that unsets the rightmost set bit of an integer.

**Examples :**

Input: 12 (00...01100) Output: 8 (00...01000) Input: 7 (00...00111) Output: 6 (00...00110)

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

## C

`#include <stdio.h> ` ` ` `// unsets the rightmost set bit ` `// of n and returns the result ` `int` `fun(unsigned ` `int` `n) ` `{ ` ` ` `return` `n & (n - 1); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 7; ` ` ` `printf` `(` `"The number after unsetting the"` `); ` ` ` `printf` `(` `" rightmost set bit %d"` `, fun(n)); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to unset the ` `// rightmost set bit of an integer. ` ` ` `class` `GFG { ` ` ` ` ` `/* unsets the rightmost set bit ` ` ` `of n and returns the result */` ` ` `static` `int` `fun(` `int` `n) ` ` ` `{ ` ` ` `return` `n & (n - ` `1` `); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String arg[]) ` ` ` `{ ` ` ` `int` `n = ` `7` `; ` ` ` `System.out.print(` `"The number after unsetting "` ` ` `+ ` `"the rightmost set bit "` `+ fun(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

`# unsets the rightmost set bit ` `# of n and returns the result ` `def` `fun(n): ` ` ` ` ` `return` `n & (n` `-` `1` `) ` ` ` `# Driver code ` ` ` `n ` `=` `7` `print` `(` `"The number after unsetting the rightmost set bit"` `, fun(n)) ` ` ` `# This code is contributed ` `# by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to unset the ` `// rightmost set bit of an integer. ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `/* unsets the rightmost set bit ` ` ` `of n and returns the result */` ` ` `static` `int` `fun(` `int` `n) ` ` ` `{ ` ` ` `return` `n & (n - 1); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 7; ` ` ` `Console.Write(` `"The number after unsetting "` ` ` `+ ` `"the rightmost set bit "` `+ fun(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Sam007 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// unsets the rightmost set bit ` `// of n and returns the result ` `function` `fun(` `$n` `) ` `{ ` `return` `$n` `& (` `$n` `- 1); ` `} ` ` ` `// Driver Code ` `$n` `= 7; ` `echo` `"The number after unsetting the"` `. ` ` ` `" rightmost set bit "` `, fun(` `$n` `); ` ` ` `// This code is contributed by vt_m. ` ` ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

The number after unsetting the rightmost set bit 6

Please write comments if you find the above code/algorithm incorrect, or find better ways to solve the same problem

## Recommended Posts:

- How to turn on a particular bit in a number?
- How to turn off a particular bit in a number?
- Set the rightmost off bit
- Set the rightmost unset bit
- Set the rightmost unset bit
- Position of rightmost different bit
- Position of rightmost set bit
- Get the position of rightmost unset bit
- Position of rightmost bit with first carry in sum of two binary
- Position of rightmost common bit in two numbers
- XOR of a submatrix queries
- Maximum Bitwise AND pair from given range
- Iterating over all possible combinations in an Array using Bits
- Given two binary strings perform operation until B > 0 and print the result