XOR of Numbers from L to R

Implement rangeXor

Given two integers l and r, compute the XOR of every integer in that closed range — l, l + 1, all the way through r — combined into a single value. Walking the range one integer at a time works, but there's a shortcut: the XOR of every integer from 1 up to any n follows a fixed 4-step cycle based on n's remainder mod 4. Once that shortcut gives the running XOR up to any single number instantly, the range [l, r] is just the difference between two such running totals — one up to r, one up to l − 1 — since combining the same prefix with itself would cancel it out completely.

Example 1:

Input: l = 4, r = 8

Output: 8

Example 2:

Input: l = 1, r = 10

Output: 11

Example 3:

Input: l = 5, r = 5

Output: 5

+ 10 hidden test cases run on Submit.

Constraints:

  • 1 ≤ l ≤ r ≤ 10⁹

l =

4

r =

8