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