Toggle feature flag bit

Write codeEasy10 min

Your task

A feature-flag service stores a rollout state as a non-negative integer, one flag per bit. An admin clicks a switch in the UI, which must flip flag k: if bit k is 0 it becomes 1, and if it is 1 it becomes 0, leaving every other bit untouched. Given the current state (0 <= state < 2^31) and the flag index k (0 <= k <= 30), return the new state. For example, state 5 (binary 101) with k = 1 returns 7 (binary 111).

Examples

toggle_flag(state: int, k: int) -> int
Example 1
Inputtoggle_flag(5, 1)
Output7
Example 2
Inputtoggle_flag(7, 0)
Output6