Number formed by the rightmost set bit in N | Java 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
class geeks { public static int firstSetBit ( int n ) { int x = n & ( n - 1 ) ; return ( n ^ x ) ; } public static void main ( String [ ] args ) { int n = 12 ; System . out . println ( firstSetBit ( n ) ) ; } }