Smallest number exceeding N whose Kth bit is set | Function to find the number greater than n whose Kth bit is set ; Iterate from N + 1 ; Check if Kth bit is set or not ; Increment M for next number ; Return the minimum value ; Driver Code ; Given N and K ; Function Call
def find_next ( n , k ) : NEW_LINE INDENT M = n + 1 ; NEW_LINE while ( True ) : NEW_LINE INDENT if ( ( M & ( 1 << k ) ) > 0 ) : NEW_LINE INDENT break ; NEW_LINE DEDENT M += 1 ; NEW_LINE DEDENT return M ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 15 ; K = 2 ; NEW_LINE print ( find_next ( N , K ) ) ; NEW_LINE DEDENT