# Calculate XOR from 1 to n.

Given a number n, the task is to find the XOR from 1 to n.

Examples :

```Input : n = 6
Output : 7
// 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6  = 7

Input : n = 7
Output : 0
// 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7 = 0
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Method 1 (Naive Approach):
1- Initialize result as 0.
1- Traverse all numbers from 1 to n.
2- Do XOR of numbers one by one with result.
3- At the end, return result.

Method 2 (Efficient method) :
1- Find the remainder of n by moduling it with 4.
2- If rem = 0, then xor will be same as n.
3- If rem = 1, then xor will be 1.
4- If rem = 2, then xor will be n+1.
5- If rem = 3 ,then xor will be 0.

## C/C++

 `// C++ program to find XOR of numbers ` `// from 1 to n. ` `#include ` `using` `namespace` `std; ` ` `  `// Function to calculate xor ` `long` `computeXOR(``const` `int` `n) ` `{ ` `    ``// Modulus operator are expensive on most of the  ` `    ``// computers. n & 3 will be equivalent to n % 4.    ` ` `  `    ``switch``(n & 3) ``// n % 4  ` `    ``{ ` `    ``case` `0: ``return` `n;     ``// if n is multiple of 4 ` `    ``case` `1: ``return` `1;     ``// If n % 4 gives remainder 1   ` `    ``case` `2: ``return` `n + 1; ``// If n % 4 gives remainder 2     ` `    ``case` `3: ``return` `0;     ``// If n % 4 gives remainder 3   ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``// your code goes here ` `    ``int` `n = 5; ` `    ``cout <<    computeXOR(n); ` `    ``return` `0; ` `} `

## Java

 `// Java program to find XOR of numbers ` `// from 1 to n. ` ` `  `class` `GFG  ` `{ ` `    ``// Method to calculate xor ` `    ``static` `int` `computeXOR(``int` `n) ` `    ``{ ` `        ``// If n is a multiple of 4 ` `        ``if` `(n % ``4` `== ``0``) ` `            ``return` `n; ` `      `  `        ``// If n%4 gives remainder 1 ` `        ``if` `(n % ``4` `== ``1``) ` `            ``return` `1``; ` `      `  `        ``// If n%4 gives remainder 2 ` `        ``if` `(n % ``4` `== ``2``) ` `            ``return` `n + ``1``; ` `      `  `        ``// If n%4 gives remainder 3 ` `        ``return` `0``; ` `    ``} ` `     `  `    ``// Driver method ` `    ``public` `static` `void` `main (String[] args) ` `    ``{ ` `         ``int` `n = ``5``; ` `         ``System.out.println(computeXOR(n)); ` `    ``} ` `} `

## Python 3

 `# Python 3 Program to find  ` `# XOR of numbers from 1 to n.  ` ` `  `# Function to calculate xor  ` `def` `computeXOR(n) : ` ` `  `    ``# Modulus operator are expensive  ` `    ``# on most of the computers. n & 3  ` `    ``# will be equivalent to n % 4. ` ` `  `    ``# if n is multiple of 4  ` `    ``if` `n ``%` `4` `=``=` `0` `: ` `        ``return` `n ` ` `  `    ``# If n % 4 gives remainder 1 ` `    ``if` `n ``%` `4` `=``=` `1` `: ` `        ``return` `1` ` `  `    ``# If n%4 gives remainder 2  ` `    ``if` `n ``%` `4` `=``=` `2` `: ` `        ``return` `n ``+` `1` ` `  `    ``# If n%4 gives remainder 3 ` `    ``return` `0` ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"` `: ` ` `  `    ``n ``=` `5` ` `  `    ``# function calling ` `    ``print``(computeXOR(n)) ` `         `  `# This code is contributed by ANKITRAI1 `

## C#

 `// C# program to find XOR  ` `// of numbers from 1 to n. ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `    ``// Method to calculate xor ` `    ``static` `int` `computeXOR(``int` `n) ` `    ``{ ` `        ``// If n is a multiple of 4 ` `        ``if` `(n % 4 == 0) ` `            ``return` `n; ` `     `  `        ``// If n%4 gives remainder 1 ` `        ``if` `(n % 4 == 1) ` `            ``return` `1; ` `     `  `        ``// If n%4 gives remainder 2 ` `        ``if` `(n % 4 == 2) ` `            ``return` `n + 1; ` `     `  `        ``// If n%4 gives remainder 3 ` `        ``return` `0; ` `    ``} ` `     `  `    ``// Driver Code ` `    ``static` `public` `void` `Main () ` `    ``{ ` `        ``int` `n = 5; ` `        ``Console.WriteLine(computeXOR(n)); ` `    ``} ` `} ` ` `  `// This code is contributed by ajit `

## PHP

 ` `

Output :

```1
```

How does this work?
When we do XOR of numbers, we get 0 as XOR value just before a multiple of 4. This keeps repeating before every multiple of 4.

```Number Binary-Repr  XOR-from-1-to-n
1         1           
2        10           
3        11             <----- We get a 0
4       100             <----- Equals to n
5       101           
6       110           
7       111             <----- We get 0
8      1000             <----- Equals to n
9      1001           
10     1010           
11     1011            <------ We get 0
12     1100            <------ Equals to n
```

This article is contributed by Sahil Chhabra. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

15

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.