Bitwise XOR of all odd numbers from a given range
Last Updated :
09 Nov, 2023
Given an integer N, the task is to find the Bitwise XOR of all odd numbers in the range [1, N].
Examples:
Input: 11
Output: 2
Explanation: Bitwise XOR of all odd numbers up to 11 = 1 ^ 3 ^ 5 ^ 7 ^ 9 ^ 11 = 2.
Input: 10
Output: 9
Explanation: Bitwise XOR of all odd numbers up to 10 = 1 ^ 3 ^ 5 ^ 7 ^ 9 = 9.
Naive Approach: The simplest approach to solve the problem is to iterate over the range [1, N] and for every value, check if it is odd or not. Calculate Bitwise XOR of every odd element found and print it as the required result.
Below is the implementation of above approach :
C++
#include <bits/stdc++.h>
using namespace std;
void findOddXOR( int n)
{
int xorVal = 0;
for ( int i = 1; i <= n; i++) {
if (i % 2 != 0) {
xorVal ^= i;
}
}
cout << xorVal;
}
int main()
{
int N = 11;
findOddXOR(N);
return 0;
}
|
Java
public class GFG {
static void findOddXOR( int n) {
int xorVal = 0 ;
for ( int i = 1 ; i <= n; i++) {
if (i % 2 != 0 ) {
xorVal ^= i;
}
}
System.out.print(xorVal);
}
public static void main(String[] args) {
int N = 11 ;
findOddXOR(N);
}
}
|
Python3
def findOddXOR(n):
xorVal = 0
for i in range ( 1 , n + 1 ):
if i % 2 ! = 0 :
xorVal ^ = i
print (xorVal)
N = 11
findOddXOR(N)
|
C#
using System;
class Program
{
static void FindOddXOR( int n)
{
int xorVal = 0;
for ( int i = 1; i <= n; i++)
{
if (i % 2 != 0)
{
xorVal ^= i;
}
}
Console.WriteLine(xorVal);
}
static void Main()
{
int N = 11;
FindOddXOR(N);
}
}
|
Javascript
function findOddXOR(n) {
let xorVal = 0;
for (let i = 1; i <= n; i++) {
if (i % 2 !== 0) {
xorVal ^= i;
}
}
console.log(xorVal);
}
const N = 11;
findOddXOR(N);
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to use the following formula to calculate Bitwise XOR of all odd numbers less than or equal to N:
Let f(N) = 2 ^ 4 ^ 6 ^ … ^ (N − 2) ^ n and g(n) = 1 ^ 2 ^ 3 ^ … ^ (N − 2) / 2 ^ (N / 2)
=> f(N) = 2 * g(N)
Now, let k(N) = 1 ^ 2 ^ 3 ^ … ^ (N − 2) ^ N
XOR of all odd numbers less than or equal to N = k(N) ^ f(N) [Since all even numbers cancel their own bits leaving only odd numbers].
Substituting the value of f(N) = 2 * g(N), XOR of all odd numbers up to N = k(N) ^ (2 * g(N))
Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findXOR( int n)
{
switch (n & 3) {
case 0:
return n;
case 1:
return 1;
case 2:
return n + 1;
case 3:
return 0;
}
}
void findOddXOR( int n)
{
if (n % 2 == 0)
cout << ((findXOR(n))
^ (2 * findXOR(n / 2)));
else
cout << ((findXOR(n))
^ (2 * findXOR((n - 1) / 2)));
}
int main()
{
int N = 11;
findOddXOR(N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int findXOR( int n)
{
switch (n & 3 )
{
case 0 :
return n;
case 1 :
return 1 ;
case 2 :
return n + 1 ;
}
return 0 ;
}
static void findOddXOR( int n)
{
if (n % 2 == 0 )
System.out.print(((findXOR(n))
^ ( 2 * findXOR(n / 2 ))));
else
System.out.print(((findXOR(n))
^ ( 2 * findXOR((n - 1 ) / 2 ))));
}
public static void main(String[] args)
{
int N = 11 ;
findOddXOR(N);
}
}
|
Python3
def findXOR(n):
if (n % 4 = = 0 ):
return n;
elif (n % 4 = = 1 ):
return 1 ;
elif (n % 4 = = 2 ):
return n + 1 ;
elif (n % 4 = = 3 ):
return 0 ;
def findOddXOR(n):
if (n % 2 = = 0 ):
print (((findXOR(n)) ^ ( 2 * findXOR(n / / 2 ))));
else :
print (((findXOR(n)) ^ ( 2 * findXOR((n - 1 ) / / 2 ))));
if __name__ = = '__main__' :
N = 11 ;
findOddXOR(N);
|
C#
using System;
public class GFG
{
static int findXOR( int n)
{
switch (n & 3)
{
case 0:
return n;
case 1:
return 1;
case 2:
return n + 1;
}
return 0;
}
static void findOddXOR( int n)
{
if (n % 2 == 0)
Console.Write(((findXOR(n))
^ (2 * findXOR(n / 2))));
else
Console.Write(((findXOR(n))
^ (2 * findXOR((n - 1) / 2))));
}
public static void Main(String[] args)
{
int N = 11;
findOddXOR(N);
}
}
|
Javascript
<script>
function findXOR(n)
{
switch (n & 3) {
case 0:
return n;
case 1:
return 1;
case 2:
return n + 1;
case 3:
return 0;
}
}
function findOddXOR(n)
{
if (n % 2 == 0)
document.write((findXOR(n))
^ (2 * findXOR(n / 2)));
else
document.write((findXOR(n))
^ (2 * findXOR((n - 1) / 2)));
}
let N = 11;
findOddXOR(N);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...