1281. Find XOR of All Numbers in a Range [L, R]

EasyBit ManipulationBit ManipulationXORPrefix XOR

Given integers L and R with 1 <= L <= R, compute the XOR of all integers in the inclusive range [L, R]. Use prefix XOR: xor(1..R) ^ xor(1..L-1).

Input: A JSON object {"l": <integer>, "r": <integer>} with 1 <= l <= r.

Output: Return the XOR of the range as an integer.

Examples

Example 1
Input: {"l":3,"r":5}
Output: 2
Explanation: f(5)=1 and f(2)=3, so 1 ^ 3 = 2.
Example 2
Input: {"l":1,"r":4}
Output: 4
Explanation: f(4)=4 and f(0)=0, so the answer is 4.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →