Check if two numbers are equal without using arithmetic and comparison operators

In this blog post, I will teach you how to write C/C++ program to check if two numbers are equal without using arithmetic and comparison operators. After reading this detail guide post I believe you able to write an efficient program to determine whether two integers are equal without using comparison operators (==,!=, <, >, <=, >=) and arithmetic operators (+, -, *, /, %).

Example-1:

Input:  x= 5, y= 13


Output: Not equal

 

Example-2:

Input:  x= 15, y= 15


Output: Equal

 

Let’s see the problem statements for this question.

Problem Statement: Given two integers, x and y, the task is to check whether ‘x’ and ‘y’ are equal or not without using Arithmetic and Comparison Operators or String functions.

 

Disclaimer: Try to solve the problem yourself first otherwise it would be not worth solving this problem

Approach-1: Using Bitwise XOR Operator:

This approach is one of the easiest ways to check whether two numbers are equal or not without using arithmetic or comparison operators.

Here we use the basic properties of XOR i.e.  XOR of two numbers is 0 if the numbers are the same, otherwise non-zero.  That means (x^y) is 0, if x and y both are equal.

 

C program to check if two numbers are equal without using arithmetic and comparison operators:

/*
Check if two numbers are equal without
using arithmetic and comparison operators
*/
#include <stdio.h>
#include<stdbool.h>

// Function to check if two
// numbers are equal using
// XOR operator
bool areSame(int n1, int n2)
{
    const bool bothAreSame = (n1^n2)?false:true;
    return  bothAreSame;
}

int main()
{
    int n1 = 5, n2 = 5;

    const bool same = areSame(n1,n2);
    if(same)
    {
        printf("Numbers are same\n");
    }
    else
    {
        printf("Numbers are not same\n");
    }

    return 0;
}

Output: Numbers are same

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

 

Explanation:

In the above code, expression (n1 ^ n2) performs the XOR operation between n1 and n2. It evaluates non-zero value if both numbers are not same otherwise zero.

So, the function areSame() returns true if the expression (n1 ^ n2) is zero otherwise returns false.

 

C++ program to check if two numbers are equal without using arithmetic and comparison operators:

/*
C++ program to check if two numbers are equal without
using arithmetic and comparison operators
*/
#include <iostream>
using std::cout;
using std::endl;

// Function to check if two
// numbers are equal using
// XOR operator
bool areSame(int n1, int n2)
{
    const bool bothAreSame = (n1^n2)?false:true;
    return  bothAreSame;
}

int main()
{
    int n1 = 5, n2 = 5;

    const bool same = areSame(n1,n2);
    if(same)
    {
        cout<<"Numbers are same"<<endl;
    }
    else
    {
        cout<<"Numbers are not same"<<endl;
    }
    return 0;
}

Output: Numbers are same

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

 

Approach-2: Using Array Index + Ternary Operator

You can also check the equality of two number using temporary array, but “it is not suggested to use this approach“.  Here idea is to use the first number as the array index and set the a predefine value 0x7FFFFFFF. Then, check the same value using the second element as the array index. If you get the same value 0x7FFFFFFF that means both numbers are equal.

/*
C program to Check if two numbers are equal
without using arithmetic and comparison operators
*/
#include <stdio.h>
#include<stdbool.h>

#define MAGIC_NUMBER 0x7FFFFFFF


/*
Function to determine if two integers are equal
without using comparison and arithmetic operators
*/
bool areSame(int x, int y)
{
    int tmp[x+1];
    tmp[x] = MAGIC_NUMBER;

    const bool bothAreSame = (tmp[y] == MAGIC_NUMBER)? true :false;

    return  bothAreSame;
}

int main()
{
    int n1 = 5, n2 = 7;

    const bool same = areSame(n1,n2);
    if(same)
    {
        printf("Numbers are same\n");
    }
    else
    {
        printf("Numbers are not same\n");
    }

    return 0;
}

Output: Numbers are not same

 

Approach-3: Simple Subtraction

If you allowed to use arithmetic operators, then this is very simple approach to check the equality of two given numbers. In the approach you just need subtract one number from the another given number if the result is zero that means both numbers are equal.

/*
C program to Check if two numbers are equal
without using arithmetic and comparison operators
*/
#include <stdio.h>
#include<stdbool.h>


/* Determine if two integers are equal
without using comparison operators
*/
bool areSame(int x, int y)
{
    const bool bothAreSame  = !(x-y);

    return bothAreSame;
}

int main()
{
    int n1 = 5, n2 = 5;

    const bool same = areSame(n1,n2);
    if(same)
    {
        printf("Numbers are same\n");
    }
    else
    {
        printf("Numbers are not same\n");
    }

    return 0;
}

Output: Numbers are same

Recommended Post