Dsa
Bit Manipulation
Hard
Toggle Bits in Subrange
Given an integer `n` and two integers `l`, `r` representing the 0-based inclusive range of bits, toggle the bits in the range [l, r] of `n`. You need to implement a function that reads these inputs and outputs the new integer after toggling the specified bits.
Example:
Input:
29 1 3
Output:
21
// Explanation: 29 in binary is 11101. Toggling bits from index 1 to 3 results in 10101, which is 21 in decimal.
Key concepts
bit_manipulationrangesbinary
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