Dsa
Bit Manipulation
Easy
Toggle Bits In Range
Write a function that toggles (flips) the bits in a given integer within a specified range. The range is defined by two indices: start and end inclusive. Toggling means changing 0 to 1 and 1 to 0.
Input Format:
- The first line contains an integer x (0 ≤ x ≤ 2^31 - 1), the integer to toggle.
- The second line contains two integers, start and end (0 ≤ start ≤ end < 31).
Output Format:
- Print the new integer after toggling the bits in the specified range.
Example Input:
5
1 3
Example Output:
2
(5 in binary is 101, toggling bits 1-3 gives 010 which is 2).
Key concepts
bit_manipulationrangestoggling
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