# Number formed by the rightmost set bit in N

Given an integer **N**, the task is to find an integer **M** formed by taking the rightmost set bit in **N** i.e. the only set bit in **M** will be the rightmost set bit in **N** nd the rest of the bits will be unset.

**Examples:**

Input:N = 7

Output:1

7 = 111, the number formed by the last set bit is 001 i.e. 1.

Input:N = 10

Output:2

10 = 1010 -> 0010 = 2

Input:N = 16

Output:16

**Approach:**

- Store
**x = n & (n – 1)**which will unset the first set bit from the right in**n**. - Now, update
**n = n ^ x**to set the changed bit and unset all the others which is the required integer.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the integer formed ` `// by taking the rightmost set bit in n ` `int` `firstSetBit(` `int` `n) ` `{ ` ` ` ` ` `// n & (n - 1) unsets the first set ` ` ` `// bit from the right in n ` ` ` `int` `x = n & (n - 1); ` ` ` ` ` `// Take xor with the original number ` ` ` `// The position of the 'changed bit' ` ` ` `// will be set and rest will be unset ` ` ` `return` `(n ^ x); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 12; ` ` ` ` ` `cout << firstSetBit(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `geeks ` `{ ` ` ` ` ` `// Function to return the integer formed ` ` ` `// by taking the rightmost set bit in n ` ` ` `public` `static` `int` `firstSetBit(` `int` `n) ` ` ` `{ ` ` ` ` ` `// n & (n - 1) unsets the first set ` ` ` `// bit from the right in n ` ` ` `int` `x = n & (n-` `1` `); ` ` ` ` ` `// Take xor with the original number ` ` ` `// The position of the 'changed bit' ` ` ` `// will be set and rest will be unset ` ` ` `return` `(n ^ x); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `12` `; ` ` ` `System.out.println(firstSetBit(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by ` `// sanjeev2552 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the integer formed ` `# by taking the rightmost set bit in n ` `def` `firstSetBit(n): ` ` ` ` ` `# n & (n - 1) unsets the first set ` ` ` `# bit from the right in n ` ` ` `x ` `=` `n & (n ` `-` `1` `) ` ` ` ` ` `# Take xor with the original number ` ` ` `# The position of the 'changed bit' ` ` ` `# will be set and rest will be unset ` ` ` `return` `(n ^ x) ` ` ` `# Driver code ` `n ` `=` `12` ` ` `print` `(firstSetBit(n)) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `geeks ` `{ ` ` ` ` ` `// Function to return the integer formed ` ` ` `// by taking the rightmost set bit in n ` ` ` `public` `static` `int` `firstSetBit(` `int` `n) ` ` ` `{ ` ` ` ` ` `// n & (n - 1) unsets the first set ` ` ` `// bit from the right in n ` ` ` `int` `x = n & (n-1); ` ` ` ` ` `// Take xor with the original number ` ` ` `// The position of the 'changed bit' ` ` ` `// will be set and rest will be unset ` ` ` `return` `(n ^ x); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `n = 12; ` ` ` `Console.WriteLine(firstSetBit(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by ` `// anuj_67.. ` |

*chevron_right*

*filter_none*

**Output:**

4

## Recommended Posts:

- Find maximum number that can be formed using digits of a given number
- Largest even number that can be formed by any number of swaps
- Number of triangles that can be formed
- Number of triangles that can be formed with given N points
- Check if a number is formed by Concatenation of 1, 14 or 144 only
- Check if the large number formed is divisible by 41 or not
- Number of triangles formed from a set of points on three lines
- Greatest number less than equal to B that can be formed from the digits of A
- Maximum number of teams that can be formed with given persons
- Length of the smallest number which is divisible by K and formed by using 1's only
- Check if the number formed by the last digits of N numbers is divisible by 10 or not
- Find the largest number that can be formed by changing at most K digits
- Minimum number of consecutive sequences that can be formed in an array
- Find Nth even length palindromic number formed using digits X and Y
- Recursive sum of digits of a number formed by repeated appends

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.