Toggle Kth Bit
Python
Easy
3 views
Problem Description
Read two integers n and k. Toggle (flip) the k-th bit (0-based) of n and output the new number.
Input Format
One line: n k.
Output Format
One integer newN.
Official Solution
import sys
p=sys.stdin.read().strip().split()
if len(p)<2: sys.exit(0)
n=int(p[0]); k=int(p[1])
mask=1<<k
sys.stdout.write(str(n ^ mask))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!