Set the Left most unset bit | Set left most unset bit ; If number contain all 1 then return n ; Find position of leftmost unset bit . ; If temp L . S . B is zero then unset bit pos is change ; Return OR of number and unset bit pos ; Driver Code
function setleftmostunsetbit ( n ) { if ( ( n & ( n + 1 ) ) == 0 ) return n ; let pos = 0 ; for ( let temp = n , count = 0 ; temp > 0 ; temp >>= 1 , count ++ ) if ( ( temp & 1 ) == 0 ) pos = count ; return ( n | ( 1 << ( pos ) ) ) ; } let n = 10 ; document . write ( setleftmostunsetbit ( n ) ) ;