Number formed by the rightmost set bit in N | 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
function firstSetBit ( n ) { let x = n & ( n - 1 ) ; return ( n ^ x ) ; } let n = 12 ; document . write ( firstSetBit ( n ) ) ;