human
Smallest number exceeding N whose Kth bit is set | Function to find the number greater than n whose Kth bit is set.
Stores the resultant number.
If Kth bit is not set.
cur will be the sum of all powers of 2 < k.
If the current bit is set.
Add Kth power of 2 to n and subtract the all powers of 2 less than K that are set.
If the kth bit is set.
First unset bit position.
Sum of bits that are set.
Add Kth power of 2 to n and subtract the all powers of 2 less than K that are set.
If Kth bit became unset then set it again.
Return the resultant number.
Driver code.
Print ans"
How can the above be solved in Python?
 Write me a code to smallest number exceeding N whose Kth bit is set and the other steps required?
assistant
#Here is code to smallest number exceeding N whose Kth bit is set and the other steps required.
 def find_next ( n , k ) :
	ans = 0
	if ( ( n & ( 1 << k ) ) == 0 ) :
		cur = 0
		for i in range ( k ) :
			if ( n & ( 1 << i ) ) :
				cur += 1 << i
		ans = n - cur + ( 1 << k )
	else :
		first_unset_bit , cur = - 1 , 0
		for i in range ( 64 ) :
			if ( ( n & ( 1 << i ) ) == 0 ) :
				first_unset_bit = i
				break
			else :
				cur += ( 1 << i )
		ans = n - cur + ( 1 << first_unset_bit )
		if ( ( ans & ( 1 << k ) ) == 0 ) :
			ans += ( 1 << k )
	return ans
N , K = 15 , 2
print ( find_next ( N , K ) )