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 Function
def setleftmostunsetbit ( n ) : NEW_LINE INDENT if not ( n & ( n + 1 ) ) : NEW_LINE INDENT return n NEW_LINE DEDENT pos , temp , count = 0 , n , 0 NEW_LINE while temp : NEW_LINE INDENT if not ( temp & 1 ) : NEW_LINE INDENT pos = count NEW_LINE DEDENT count += 1 ; temp >>= 1 NEW_LINE DEDENT return ( n | ( 1 << ( pos ) ) ) NEW_LINE DEDENT n = 10 NEW_LINE print ( setleftmostunsetbit ( n ) ) NEW_LINE