Posted on by Kalkicode
Code Bit Logic

# Xor of two numbers without using XOR operator

The XOR operation, also known as exclusive OR, is a binary operation that takes two bits and returns 1 if exactly one of the bits is 1, otherwise 0. In many programming languages, the XOR operator (^) is used to perform the XOR operation on integers. However, in this article, we will explore an alternative method to calculate the XOR of two numbers without using the XOR operator. We will achieve this by using bitwise NOT (~) and bitwise AND (&) operators.

## Problem Statement

Given two integers, x and y, the task is to find their XOR without using the XOR operator.

## Explanation with Suitable Example

Let's consider two integers, x = 5 (binary: 0101) and y = 3 (binary: 0011), as an example to demonstrate the approach.

## Standard Pseudocode

``````function xorOperation(x, y):
result = ((~x) & y) | (x & (~y))
return result
``````

Algorithm Explanation:

1. First, we negate the bits of both numbers using the bitwise NOT operator (~). This operation is equivalent to flipping all the bits of the number.
2. Then, we perform a bitwise AND operation between the negated x and the original y, and also between the original x and the negated y.
3. Next, we combine the results of the two bitwise AND operations using the bitwise OR operator (|). This step sets the bits that are different in the two numbers to 1 and keeps the bits that are the same as 0.
4. The final result obtained after these operations will be the XOR of the two numbers.

## Code Solution

Here given code implementation process.

``````// C Program
// Xor of two numbers without using XOR operator
#include <stdio.h>

// Perform xor operation of two integers
void xorOperation(int x, int y)
{
// Calculate XOR
int result = ((~x) & y) | (x & (~y));
// Display calculated result
printf(" ((%d) ^ (%d)) : %d \n", x, y, result);
}
int main()
{
// Test cases
xorOperation(2, 5);
xorOperation(6, 4);
xorOperation(8, 3);
return 0;
}``````

#### Output

`````` ((2) ^ (5)) : 7
((6) ^ (4)) : 2
((8) ^ (3)) : 11``````
``````/*
Java Program
Xor of two numbers without using XOR operator
*/
public class Operation
{

// Perform xor operation of two integers
public void xorOperation(int x, int y)
{
// Calculate XOR
int result = ((~x ) & y) | ( x & (~y) ) ;
// Display calculated result
System.out.print(" ((" + x + ") ^ (" + y + ")) : " + result + " \n");
}
public static void main(String[] args)
{
// Test cases
}
}``````

#### Output

`````` ((2) ^ (5)) : 7
((6) ^ (4)) : 2
((8) ^ (3)) : 11``````
``````// Include header file
#include <iostream>
using namespace std;

/*
C++ Program
Xor of two numbers without using XOR operator
*/

class Operation
{
public:
// Perform xor operation of two integers
void xorOperation(int x, int y)
{
// Calculate XOR
int result = ((~x) & y) | (x & (~y));
// Display calculated result
cout << " ((" << x << ") ^ (" << y << ")) : " << result << " \n";
}
};
int main()
{
// Test cases
return 0;
}``````

#### Output

`````` ((2) ^ (5)) : 7
((6) ^ (4)) : 2
((8) ^ (3)) : 11``````
``````// Include namespace system
using System;
/*
C# Program
Xor of two numbers without using XOR operator
*/
public class Operation
{
// Perform xor operation of two integers
public void xorOperation(int x, int y)
{
// Calculate XOR
int result = ((~x) & y) | (x & (~y));
// Display calculated result
Console.Write(" ((" + x + ") ^ (" + y + ")) : " + result + " \n");
}
public static void Main(String[] args)
{
// Test cases
}
}``````

#### Output

`````` ((2) ^ (5)) : 7
((6) ^ (4)) : 2
((8) ^ (3)) : 11``````
``````<?php
/*
Php Program
Xor of two numbers without using XOR operator
*/
class Operation
{
// Perform xor operation of two integers
public	function xorOperation(\$x, \$y)
{
// Calculate XOR
\$result = ((~\$x) & \$y) | (\$x & (~\$y));
// Display calculated result
echo " ((". \$x .") ^ (". \$y .")) : ". \$result ." \n";
}
}

function main()
{
// Test cases
}
main();``````

#### Output

`````` ((2) ^ (5)) : 7
((6) ^ (4)) : 2
((8) ^ (3)) : 11``````
``````/*
Node Js Program
Xor of two numbers without using XOR operator
*/
class Operation
{
// Perform xor operation of two integers
xorOperation(x, y)
{
// Calculate XOR
var result = ((~x) & y) | (x & (~y));
// Display calculated result
process.stdout.write(" ((" + x + ") ^ (" + y + ")) : " + result + " \n");
}
}

function main()
{
// Test cases
}
main();``````

#### Output

`````` ((2) ^ (5)) : 7
((6) ^ (4)) : 2
((8) ^ (3)) : 11``````
``````#   Python 3 Program
#   Xor of two numbers without using XOR operator

class Operation :
#  Perform xor operation of two integers
def xorOperation(self, x, y) :
#  Calculate XOR
result = ((~x) & y) | (x & (~y))
#  Display calculated result
print(" ((", x ,") ^ (", y ,")) : ", result )

def main() :
#  Test cases

if __name__ == "__main__": main()``````

#### Output

`````` (( 2 ) ^ ( 5 )) :  7
(( 6 ) ^ ( 4 )) :  2
(( 8 ) ^ ( 3 )) :  11``````
``````#   Ruby Program
#   Xor of two numbers without using XOR operator

class Operation
#  Perform xor operation of two integers
def xorOperation(x, y)
#  Calculate XOR
result = ((~x) & y) | (x & (~y))
#  Display calculated result
print(" ((", x ,") ^ (", y ,")) : ", result ," \n")
end

end

def main()
#  Test cases
end

main()``````

#### Output

`````` ((2) ^ (5)) : 7
((6) ^ (4)) : 2
((8) ^ (3)) : 11
``````
``````/*
Scala Program
Xor of two numbers without using XOR operator
*/
class Operation
{
// Perform xor operation of two integers
def xorOperation(x: Int, y: Int): Unit = {
// Calculate XOR
var result: Int = ((~x) & y) | (x & (~y));
// Display calculated result
print(" ((" + x + ") ^ (" + y + ")) : " + result + " \n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Operation = new Operation();
// Test cases
}
}``````

#### Output

`````` ((2) ^ (5)) : 7
((6) ^ (4)) : 2
((8) ^ (3)) : 11``````
``````/*
Swift 4 Program
Xor of two numbers without using XOR operator
*/
class Operation
{
// Perform xor operation of two integers
func xorOperation(_ x: Int, _ y: Int)
{
// Calculate XOR
let result: Int = ((~x) & y) | (x & (~y));
// Display calculated result
print(" ((", x ,") ^ (", y ,")) : ", result ," ");
}
}
func main()
{
// Test cases
}
main();``````

#### Output

`````` (( 2 ) ^ ( 5 )) :  7
(( 6 ) ^ ( 4 )) :  2
(( 8 ) ^ ( 3 )) :  11``````
``````/*
Kotlin Program
Xor of two numbers without using XOR operator
*/
class Operation
{
// Perform xor operation of two integers
fun xorOperation(x: Int, y: Int): Unit
{
// Calculate XOR
var result: Int = ((x.inv()) and y) or(x and(y.inv()));
// Display calculated result
print(" ((" + x + ") ^ (" + y + ")) : " + result + " \n");
}
}
fun main(args: Array < String > ): Unit
{
// Test cases
}``````

#### Output

`````` ((2) ^ (5)) : 7
((6) ^ (4)) : 2
((8) ^ (3)) : 11``````

## Resultant Output Explanation

```Let's apply the algorithm to the example values:
x = 5 (binary: 0101) and y = 3 (binary: 0011)```

Step 1: ~x = ~0101 = 1010 (binary)

~y = ~0011 = 1100 (binary)

Step 2: 1010 & 0011 = 0000 (binary)

0101 & 1100 = 0100 (binary)

Step 3: 0000 | 0100 = 0100 (binary)

Therefore, the XOR of 5 and 3 is 4.

## Time Complexity

The time complexity of this algorithm is O(1) because the bitwise operations are constant-time operations that do not depend on the size of the input integers. The algorithm uses a fixed number of bitwise operations to calculate the XOR. Thus, the time complexity remains constant irrespective of the input values.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post