Dsa
Bit Manipulation
Medium
K-th Bit Flipper
You are given an integer n and an integer k. Your task is to flip the k-th bit (0-indexed from the right) of the binary representation of n. If k is greater than or equal to the number of bits in the binary representation of n, just return n.
Input:
An integer n (0 <= n <= 10^6) followed by an integer k (0 <= k < 20).
Output:
An integer representing the new value of n after flipping the k-th bit.
Example:
Input:
5 1
Output:
7
Explanation:
The binary representation of 5 is '101'. Flipping the 1st bit gives '111' which is 7.
Key concepts
bit manipulationposition indexingbitwise operations
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now