Compare two numbers without using comparison operator

The given problem is to compare two numbers without using any comparison operators like greater than, less than, equal to, etc. We need to find a way to determine whether the two numbers are equal or not using bitwise operations. In this scenario, we can use the XOR (^) bitwise operator to achieve the comparison.

Explanation with Example

Let's take an example to understand how XOR can be used to compare two numbers without using comparison operators. Consider the numbers 5 and 7 in binary representation:

``````5  ->  0101
7  ->  0111
``````

Now, let's apply the XOR operation on these two numbers:

``(5 ^ 7) -> (0101 ^ 0111) -> 0010``

The result of the XOR operation is `0010`, which is `2` in decimal representation. We can observe that the result of XOR represents the positions where the two numbers have different bits. If the two numbers are equal, the XOR result will be `0`.

Standard Pseudocode:

``````function isEqual(x, y):
result = x XOR y
if result is 0:
return true
else:
return false``````

Algorithm Explanation

1. Define a function `isEqual(x, y)` that takes two integer inputs, `x` and `y`.
2. Calculate the result of XOR operation between `x` and `y` and store it in a variable called `result`.
3. Check if the `result` is equal to `0`.
4. If `result` is `0`, return `true`, indicating that the numbers `x` and `y` are equal.
5. If `result` is not `0`, return `false`, indicating that the numbers `x` and `y` are not equal.

Code Solution

Here given code implementation process.

``````//  C program
//  Compare two numbers without using comparison operator
#include <stdio.h>

// Compare two numbers
void isEqual(int x, int y)
{
// Compare two number
int result = !(x ^ y);
// Display given number
printf("\n Number X : %d", x);
printf("\n Number Y : %d", y);
// Display calculated result
// 0 indicates false and 1 indicates true
printf("\n Result : %d", result);
}
int main(int argc, char
const *argv[])
{
// Test case
isEqual(2, 5);
isEqual(3, 3);
isEqual(-1, 1);
return 0;
}``````

Output

`````` Number X : 2
Number Y : 5
Result : 0
Number X : 3
Number Y : 3
Result : 1
Number X : -1
Number Y : 1
Result : 0``````
``````// Include header file
#include <iostream>

using namespace std;
/*
C++ program
Compare two numbers without using comparison operator
*/
class Equality
{
public:
// Compare two numbers
void isEqual(int x, int y)
{
// Display given number
cout << "\n Number X : " << x;
cout << "\n Number Y : " << y;
// Compare two number
// 0 indicates false and 1 indicates true
int result = !(x ^ y);
cout << "\n Result : " << result;
}
};
int main()
{
// Test case
return 0;
}``````

Output

`````` Number X : 2
Number Y : 5
Result : 0
Number X : 3
Number Y : 3
Result : 1
Number X : -1
Number Y : 1
Result : 0``````
``````<?php
/*
Php program
Compare two numbers without using comparison operator
*/
class Equality
{
// Compare two numbers
public
function isEqual(\$x, \$y)
{
// Display given number
echo "\n Number X : ".\$x;
echo "\n Number Y : ".\$y;
// Compare two number
// 0 indicates false and 1 indicates true
if (\$x ^ \$y)
{
echo "\n Result : 0";
}
else
{
echo "\n Result : 1";
}
}
}

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

Output

`````` Number X : 2
Number Y : 5
Result : 0
Number X : 3
Number Y : 3
Result : 1
Number X : -1
Number Y : 1
Result : 0``````
``````/*
Node Js program
Compare two numbers without using comparison operator
*/
class Equality
{
// Compare two numbers
isEqual(x, y)
{
// Display given number
process.stdout.write("\n Number X : " + x);
process.stdout.write("\n Number Y : " + y);
// Compare two number
var result = !(x ^ y);
process.stdout.write("\n Result : " + result);
}
}

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

Output

`````` Number X : 2
Number Y : 5
Result : false
Number X : 3
Number Y : 3
Result : true
Number X : -1
Number Y : 1
Result : false``````
``````#   Python 3 program
#   Compare two numbers without using comparison operator

class Equality :
#  Compare two numbers
def isEqual(self, x, y) :
#  Display given number
print("\n Number X : ", x, end = "")
print("\n Number Y : ", y, end = "")
#  Compare two number
result = not(x ^ y)
print("\n Result : ", result, end = "")

def main() :
#  Test case

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

Output

`````` Number X :  2
Number Y :  5
Result :  False
Number X :  3
Number Y :  3
Result :  True
Number X :  -1
Number Y :  1
Result :  False``````
``````#   Ruby program
#   Compare two numbers without using comparison operator

class Equality
#  Compare two numbers
def isEqual(x, y)
#  Display given number
print("\n Number X : ", x)
print("\n Number Y : ", y)
#  Compare two number
#  0 indicates false and 1 indicates true
if((x ^ y) != 0)
print("\n Result : ", 0)
else
print("\n Result : ", 1)
end

end

end

def main()
#  Test case
end

main()``````

Output

`````` Number X : 2
Number Y : 5
Result : 0
Number X : 3
Number Y : 3
Result : 1
Number X : -1
Number Y : 1
Result : 0``````
``````/*
Scala program
Compare two numbers without using comparison operator
*/
class Equality
{
// Compare two numbers
def isEqual(x: Int, y: Int): Unit = {
// Display given number
print("\n Number X : " + x);
print("\n Number Y : " + y);
// Compare two number
// 0 indicates false and 1 indicates true
if ((x ^ y) != 0)
{
print("\n Result : " + 0);
}
else
{
print("\n Result : " + 1);
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Equality = new Equality();
// Test case
}
}``````

Output

`````` Number X : 2
Number Y : 5
Result : 0
Number X : 3
Number Y : 3
Result : 1
Number X : -1
Number Y : 1
Result : 0``````
``````/*
Swift 4 program
Compare two numbers without using comparison operator
*/
class Equality
{
// Compare two numbers
func isEqual(_ x: Int, _ y: Int)
{
// Display given number
print("\n Number X : ", x, terminator: "");
print("\n Number Y : ", y, terminator: "");
// Compare two number
// 0 indicates false and 1 indicates true
if ((x ^ y)  != 0)
{
print("\n Result : 0", terminator: "");
}
else
{
print("\n Result : 1", terminator: "");
}
}
}
func main()
{
// Test case
}
main();``````

Output

`````` Number X :  2
Number Y :  5
Result : 0
Number X :  3
Number Y :  3
Result : 1
Number X :  -1
Number Y :  1
Result : 0``````
``````/*
Kotlin program
Compare two numbers without using comparison operator
*/
class Equality
{
// Compare two numbers
fun isEqual(x: Int, y: Int): Unit
{
// Display given number
print("\n Number X : " + x);
print("\n Number Y : " + y);
// Compare two number
// 0 indicates false and 1 indicates true
if ((x xor y) != 0)
{
print("\n Result : 0");
}
else
{
print("\n Result : 1");
}
}
}
fun main(args: Array < String > ): Unit
{
// Test case
}``````

Output

`````` Number X : 2
Number Y : 5
Result : 0
Number X : 3
Number Y : 3
Result : 1
Number X : -1
Number Y : 1
Result : 0``````
``````/*
Java program
Compare two numbers without using comparison operator
*/
public class Equality
{
// Compare two numbers
public void isEqual(int x, int y)
{
// Display given number
System.out.print("\n Number X : " + x);
System.out.print("\n Number Y : " + y);
// Compare two number
// 0 indicates false and 1 indicates true
if ((x ^ y) != 0)
{
System.out.print("\n Result : 0");
}
else
{
System.out.print("\n Result : 1");
}
}
public static void main(String[] args)
{
// Test case
}
}``````

Output

`````` Number X : 2
Number Y : 5
Result : 0
Number X : 3
Number Y : 3
Result : 1
Number X : -1
Number Y : 1
Result : 0``````
``````// Include namespace system
using System;
/*
C# program
Compare two numbers without using comparison operator
*/
public class Equality
{
// Compare two numbers
public void isEqual(int x, int y)
{
// Display given number
Console.Write("\n Number X : " + x);
Console.Write("\n Number Y : " + y);
// Compare two number
// 0 indicates false and 1 indicates true
if ((x ^ y) != 0)
{
Console.Write("\n Result : 0");
}
else
{
Console.Write("\n Result : 1");
}
}
public static void Main(String[] args)
{
// Test case
}
}``````

Output

`````` Number X : 2
Number Y : 5
Result : 0
Number X : 3
Number Y : 3
Result : 1
Number X : -1
Number Y : 1
Result : 0``````

Resultant Output Explanation

Let's apply the algorithm to the provided test cases:

1. Test case: `isEqual(2, 5)`

• Binary representation of 2: `0010`
• Binary representation of 5: `0101`
• XOR result: `0010` (Decimal: 2)
• Output: Number X: 2, Number Y: 5, Result: 0 (False)
2. Test case: `isEqual(3, 3)`

• Binary representation of 3: `0011`
• Binary representation of 3: `0011`
• XOR result: `0000` (Decimal: 0)
• Output: Number X: 3, Number Y: 3, Result: 1 (True)
3. Test case: `isEqual(-1, 1)`

• Binary representation of -1: `1111 1111` (Assuming 8-bit representation)
• Binary representation of 1: `0000 0001`
• XOR result: `1111 1110` (Decimal: -2)
• Output: Number X: -1, Number Y: 1, Result: 0 (False)

Time Complexity:

The time complexity of this algorithm is O(1), which means it is constant time. Regardless of the magnitude of the input numbers, the algorithm performs a fixed number of bitwise operations, making it highly efficient. The XOR operation takes a constant amount of time for any input size. Therefore, the time complexity is constant O(1).

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.