Bit Range Toggle

Bit Manipulation Bitwise Toggle Easy Coding Challenge

Problem Description

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.

Example

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:

  • Bit at position 1: 0 becomes 1
  • Bit at position 2: 1 becomes 0
  • Bit at position 3: 1 becomes 0

The new binary representation becomes 10011 (which is 19 in decimal), so the function should return 19.

Starter Code Instructions

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!