Dsa
Bit Manipulation
Medium
Toggle Bits in Range
You are given an integer X and a range represented by two integers L and R. Write a program to toggle all the bits of X from position L to R (1-indexed). Toggling a bit means changing it from 1 to 0 or from 0 to 1. Print the modified integer after toggling.
Input format:
- The first line contains an integer X (0 <= X <= 10^9).
- The second line contains two integers L and R (1 <= L <= R <= 32).
Output format:
- Print the integer after toggling the specified bits.
Example:
Input:
5
2 4
Output:
14
(5 in binary is 101, toggling bits 2 to 4 results in 1110, which is 14 in decimal)
Key concepts
bit_manipulationranges
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