Given a non-negative integer n
and two integers i
and j
(with 0 <= i <= j
), toggle (i.e., flip from 0 to 1 or vice versa) all the bits of n
in the inclusive range from bit position i
to bit position j
. Positions are counted from 0 starting at the least-significant bit (LSB).
Your task is to implement a function that performs this toggle and returns the resulting integer.
For example, if n = 29
(which is 11101
in binary), i = 1
, and j = 3
, toggling the bits at positions 1, 2, and 3 transforms n
as follows:
The new binary representation becomes 10011
(which is 19
in decimal), so the function should return 19
.
Below is some language-agnostic starter code that outlines the function signature and comments to guide you in implementing your solution. Choose your preferred programming language from the list below.
Happy coding and enjoy exploring bit manipulation on this fine winter day!