Related Articles

# Turn off the rightmost set bit | Set 2

• Difficulty Level : Easy
• Last Updated : 26 Mar, 2021

Given a Number N. The task is to unset the rightmost set bit of N.
Examples:

```Input: N = 5
Output: 4
Explanation:
101(5) -> 100(4)

Input : N = 78
Output : 76
Explanation:
1001110(78) -> 1001100(76)```

Naive Approach:

• A simple approach to flip the rightmost set bit is to find the position of the rightmost set bit in the number by a bitwise right shift operation to check for the first occurrence of 1.

• Then, flip the bit in this position. Flipping can be done by applying XOR of the given number and the number with the bit in this position set.
Important property to flip bits:

```0 ^ 1 = 1
1 ^ 1 = 0

Xor with 1 flips the bit.```
•
• Setting only a given bit can be done by taking 2 to the power of position to set a particular bit.

Below is the implementation of the above approach.

## C++

 `// C++ program to unset the rightmost``// set bit``#include ``using` `namespace` `std;` `// Unsets the rightmost set bit ``// of n and returns the result``void` `FlipBits(``int` `n)``{` `    ``for` `(``int` `bit = 0; bit < 32; bit++)``    ``{``        ``// Checking whether bit position is``        ``// set or not``        ``if` `((n >> bit) & 1)``        ``{``            ``// If bit position is found set,``            ``// we flip this bit by xoring``            ``// given number and number with``            ``// bit position set``            ``n = n ^ (1ll << bit);``            ``break``;``        ``}``    ``}``    ` `    ``cout<<``"The number after unsetting the"``; ``    ``cout<<``" rightmost set bit "``<< n; ` `}``// Driver code``int` `main()``{``    ` `    ``int` `N = 12;``    ` `    ``FlipBits(N);``    ``return` `0;``}`

## Java

 `// Java program to unset the rightmost``// set bit``import` `java.util.*;` `class` `GFG{` `// Unsets the rightmost set bit``// of n and returns the result``static` `void` `FlipBits(``int` `n)``{``    ``for``(``int` `bit = ``0``; bit < ``32``; bit++)``    ``{``       ` `       ``// Checking whether bit position ``       ``// is set or not``       ``if` `((n >> bit) % ``2` `> ``0``)``       ``{``           ` `           ``// If bit position is found set,``           ``// we flip this bit by xoring``           ``// given number and number with``           ``// bit position set``           ``n = n ^ (``1` `<< bit);``           ``break``;``       ``}``    ``}``    ``System.out.print(``"The number after unsetting the"``);``    ``System.out.print(``" rightmost set bit "` `+ n);``}` `// Driver code``public` `static` `void` `main(String[] args)``{``    ``int` `N = ``12``;``    ` `    ``FlipBits(N);``}``}` `// This code is contributed by sapnasingh4991`

## Python3

 `# Python3 program to unset the rightmost``# set bit` `# Unsets the rightmost set bit``# of n and returns the result``def` `FlipBits(n):``    ` `    ``for` `bit ``in` `range``(``32``):``        ` `        ``# Checking whether bit position is``        ``# set or not``        ``if` `((n >> bit) & ``1``):``            ` `            ``# If bit position is found set,``            ``# we flip this bit by xoring``            ``# given number and number with``            ``# bit position set``            ``n ``=` `n ^ (``1` `<< bit)``            ``break``    ` `    ``print``(``"The number after unsetting the"``, end ``=` `" "``)``    ``print``(``"rightmost set bit"``, n)` `# Driver code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``N ``=` `12``;``    ` `    ``FlipBits(N)` `# This code is contributed by BhupendraSingh`

## C#

 `// C# program to unset the rightmost``// set bit``using` `System;` `class` `GFG{` `// Unsets the rightmost set bit``// of n and returns the result``static` `void` `FlipBits(``int` `n)``{``    ``for``(``int` `bit = 0; bit < 32; bit++)``    ``{``       ` `       ``// Checking whether bit position``       ``// is set or not``       ``if` `((n >> bit) % 2 > 0)``       ``{``           ` `           ``// If bit position is found set,``           ``// we flip this bit by xoring``           ``// given number and number with``           ``// bit position set``           ``n = n ^ (1 << bit);``           ``break``;``       ``}``    ``}``    ``Console.Write(``"The number after unsetting the "``);``    ``Console.Write(``"rightmost set bit "` `+ n);``}` `// Driver code``static` `void` `Main()``{``    ``int` `N = 12;``    ` `    ``FlipBits(N);``}``}` `// This code is contributed by shivanisinghss2110`

## Javascript

 ``
Output:

`The number after unsetting the rightmost set bit 8`

Efficient Approach:
We can flip the LSB in O(1). Though the naive approach also works fine, there also exists a one-liner solution to the problem. We can do the following:

1. Obtain 2’s complement of the number (simply -n gives 2’s complement)

2. Perform bitwise AND operation between number and it’s 2’s complement.

3. Subtract the above result from the given number.

4. The final result has LSB flipped.

Explaination

```             N = 1001110 (78)
2's compliment = 0110010
N & (-N) = 0000010
N - (N & (-N)) = 1001100 (76)```

## C++

 `// C++ program to unset the rightmost``// set bit``#include ``using` `namespace` `std;``  ` `// Unsets the rightmost set bit ``// of n and returns the result ``int` `FlipBits(unsigned ``int` `n) ``{ ``    ``return` `n -= (n & (-n));``} ``  ` `// Driver Code ``int` `main() ``{ ``    ``int` `N = 12; ``    ``cout<<``"The number after unsetting the"``; ``    ``cout<<``" rightmost set bit: "``<

## Java

 `// Java program to unset the rightmost set bit``import` `java.util.*;``class` `GFG{``    ` `// Unsets the rightmost set bit``// of n and returns the result``static` `int` `FlipBits(``int` `n)``{``    ``return` `n -= (n & (-n));``}``    ` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int` `N = ``12``;``    ` `    ``System.out.print(``"The number after unsetting the "``);``    ``System.out.print(``"rightmost set bit: "` `+ FlipBits(N));``}``}` `// This code is contributed by 29AjayKumar`

## Python3

 `# Python3 program to unset the rightmost set bit` `# Unsets the rightmost set bit``# of n and returns the result``def` `FlipBits(n):``    ` `    ``n ``-``=` `(n & (``-``n));``    ``return` `n;``    ` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``N ``=` `12``;` `    ``print``(``"The number after unsetting the"``, end ``=` `"");``    ``print``(``" rightmost set bit: "``, FlipBits(N));` `# This code is contributed by Rohit_ranjan`

## C#

 `// C# program to unset the rightmost set bit``using` `System;` `class` `GFG{``    ` `// Unsets the rightmost set bit``// of n and returns the result``static` `int` `FlipBits(``int` `n)``{``    ``return` `n -= (n & (-n));``}``    ` `// Driver Code``public` `static` `void` `Main(String[] args)``{``    ``int` `N = 12;``    ` `    ``Console.Write(``"The number after"` `+``                  ``"unsetting the "``);``    ``Console.Write(``"rightmost set bit: "` `+``                  ``FlipBits(N));``}``}` `// This code is contributed by 29AjayKumar`

## Javascript

 ``
Output:
`The number after unsetting the rightmost set bit: 8` My Personal Notes arrow_drop_up