Open In App

# XOR of all even numbers from a given range

Given two integers L and R, the task is to calculate Bitwise XOR of all even numbers in the range [L, R].

Examples:

Example:
Input: L = 10, R = 20
Output: 30
Explanation:
Bitwise XOR = 10 ^ 12 ^ 14 ^ 16 ^ 18 ^ 20 = 30
Therefore, the required output is 30.

Example:
Input: L = 15, R = 23
Output:
Explanation:
Bitwise XOR = 16 ^ 18 ^ 20 ^ 22 = 0
Therefore, the required output is 0.

Naive Approach:The simplest approach to solve the problem is to traverse all even numbers in the range [L, R] and print the Bitwise XOR of all the even numbers.

Time Complexity: O(R – L)
Auxiliary Space: O(1)

Efficient Approach: The above approach can be optimized based on the following observations:

If N is an even number:
2 ^ 4 … ^ (N) = 2 * (1 ^ 2 ^ … ^ (N / 2))

If N is an odd number:
2 ^ 4 … ^ (N) = 2 * (1 ^ 2 ^ … ^ ((N – 1) / 2))

Follow the steps below to solve the problem:

Below is the implementation of the above approach:

## C++

 `// C++ Implementation of the above approach``#include ``using` `namespace` `std;` `// Function to calculate XOR of``// numbers in the range [1, n]``int` `bitwiseXorRange(``int` `n)``{` `    ``// If n is divisible by 4``    ``if` `(n % 4 == 0)``        ``return` `n;` `    ``// If n mod 4 is equal to 1``    ``if` `(n % 4 == 1)``        ``return` `1;` `    ``// If n mod 4 is equal to 2``    ``if` `(n % 4 == 2)``        ``return` `n + 1;` `    ``return` `0;``}` `// Function to find XOR of even``// numbers in the range [l, r]``int` `evenXorRange(``int` `l, ``int` `r)``{` `    ``// Stores XOR of even numbers``    ``// in the range [1, l - 1]``    ``int` `xor_l;` `    ``// Stores XOR of even numbers``    ``// in the range [1, r]``    ``int` `xor_r;` `    ``// Update xor_r``    ``xor_r``        ``= 2 * bitwiseXorRange(r / 2);` `    ``// Update xor_l``    ``xor_l``        ``= 2 * bitwiseXorRange((l - 1) / 2);` `    ``return` `xor_l ^ xor_r;``}` `// Driver Code``int` `main()``{``    ``int` `l = 10;``    ``int` `r = 20;``    ``cout << evenXorRange(l, r);` `    ``return` `0;``}`

## Java

 `// Java Implementation of the above approach``class` `GFG``{``    ` `  ``// Function to calculate XOR of``  ``// numbers in the range [1, n]``  ``static` `int` `bitwiseXorRange(``int` `n)``  ``{` `    ``// If n is divisible by 4``    ``if` `(n % ``4` `== ``0``)``      ``return` `n;` `    ``// If n mod 4 is equal to 1``    ``if` `(n % ``4` `== ``1``)``      ``return` `1``;` `    ``// If n mod 4 is equal to 2``    ``if` `(n % ``4` `== ``2``)``      ``return` `n + ``1``;` `    ``return` `0``;``  ``}` `  ``// Function to find XOR of even``  ``// numbers in the range [l, r]``  ``static` `int` `evenXorRange(``int` `l, ``int` `r)``  ``{` `    ``// Stores XOR of even numbers``    ``// in the range [1, l - 1]``    ``int` `xor_l;` `    ``// Stores XOR of even numbers``    ``// in the range [1, r]``    ``int` `xor_r;` `    ``// Update xor_r``    ``xor_r``      ``= ``2` `* bitwiseXorRange(r / ``2``);` `    ``// Update xor_l``    ``xor_l``      ``= ``2` `* bitwiseXorRange((l - ``1``) / ``2``);` `    ``return` `xor_l ^ xor_r;``  ``}` `  ``// Driver Code``  ``public` `static` `void` `main (String[] args)``  ``{``    ``int` `l = ``10``;``    ``int` `r = ``20``;``    ``System.out.print(evenXorRange(l, r));   ``  ``}``}` `// This code is contributed by AnkThon`

## Python3

 `# Python3 implementation of the above approach` `# Function to calculate XOR of``# numbers in the range [1, n]``def` `bitwiseXorRange(n):` `    ``# If n is divisible by 4``    ``if` `(n ``%` `4` `=``=` `0``):``        ``return` `n` `    ``# If n mod 4 is equal to 1``    ``if` `(n ``%` `4` `=``=` `1``):``        ``return` `1` `    ``# If n mod 4 is equal to 2``    ``if` `(n ``%` `4` `=``=` `2``):``        ``return` `n ``+` `1` `    ``return` `0` `# Function to find XOR of even``# numbers in the range [l, r]``def` `evenXorRange(l, r):` `    ``# Stores XOR of even numbers``    ``# in the range [1, l - 1]``    ``#xor_l` `    ``# Stores XOR of even numbers``    ``# in the range [1, r]``    ``#xor_r` `    ``# Update xor_r``    ``xor_r ``=` `2` `*` `bitwiseXorRange(r ``/``/` `2``)` `    ``# Update xor_l``    ``xor_l ``=` `2` `*` `bitwiseXorRange((l ``-` `1``) ``/``/` `2``)` `    ``return` `xor_l ^ xor_r` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``l ``=` `10``    ``r ``=` `20``    ` `    ``print``(evenXorRange(l, r))` `# This code is contributed by mohit kumar 29`

## C#

 `// C# Implementation of the above approach``using` `System;``class` `GFG {``    ` `  ``// Function to calculate XOR of``  ``// numbers in the range [1, n]``  ``static` `int` `bitwiseXorRange(``int` `n)``  ``{`` ` `    ``// If n is divisible by 4``    ``if` `(n % 4 == 0)``      ``return` `n;`` ` `    ``// If n mod 4 is equal to 1``    ``if` `(n % 4 == 1)``      ``return` `1;`` ` `    ``// If n mod 4 is equal to 2``    ``if` `(n % 4 == 2)``      ``return` `n + 1;`` ` `    ``return` `0;``  ``}`` ` `  ``// Function to find XOR of even``  ``// numbers in the range [l, r]``  ``static` `int` `evenXorRange(``int` `l, ``int` `r)``  ``{`` ` `    ``// Stores XOR of even numbers``    ``// in the range [1, l - 1]``    ``int` `xor_l;`` ` `    ``// Stores XOR of even numbers``    ``// in the range [1, r]``    ``int` `xor_r;`` ` `    ``// Update xor_r``    ``xor_r``      ``= 2 * bitwiseXorRange(r / 2);`` ` `    ``// Update xor_l``    ``xor_l``      ``= 2 * bitwiseXorRange((l - 1) / 2);`` ` `    ``return` `xor_l ^ xor_r;``  ``}``  ` `  ``// Driver code``  ``static` `void` `Main()``  ``{``    ``int` `l = 10;``    ``int` `r = 20;``    ``Console.Write(evenXorRange(l, r));   ``  ``}``}` `// This code is contributed by divyeshrabadiya07`

## Javascript

 ``

Output:

`30`

Time Complexity: O(1).
Auxiliary Space: O(1)