Find the XOR of every other number in a given range

119 Views Asked by At

Lets say that R=15 and L=8, then how can I efficiently find:

15 XOR 13 XOR 11 XOR 9

If R=20 and L=10, then it would be:

20 XOR 18 XOR 16 XOR 14 XOR 12 XOR 10

$1<=L<=R<=10^{15}$

This might be similiar Finding XOR of all even numbers from n to m but that only covers even numbers. If R is odd then it will be all odd numbers from R to L.

I asked this question on StackOverflow previously but it was marked as duplicate and the problem wasn't exactly the same as this one.