In this post, we will discuss how to determine if two integers are equal without using comparison operators `(==, !=, <, >, <=, >=)` & arithmetic operators `(+, -, *, /, %)`.

### 1. Using Bitwise XOR operator

The simplest solution is to use Bitwise XOR operator. We know that for equal numbers, the XOR operator returns 0. We can make use of this fact as shown below:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <stdio.h> // Determine if two integers are equal without using comparison // and arithmetic operators int checkForEquality(int x, int y) { return !(x ^ y); } int main(void) { int x = 10, y = 10; if (checkForEquality(x, y)) printf ("x=%d is equal to y=%d\n", x, y); else printf ("x=%d is not equal to y=%d\n", x, y); return 0; } |

`Output: `

x=10 is equal to y=10

### 2. Using Array Index + Ternary Operator

We can also take advantage of the fact that by default a garbage value is assigned to a local array in C. The idea is to use first number as array index and set the value to 0. Then we simply check if array is set for second number or not.

Please note that this solution won’t work on negative numbers, consumes a lot of memory, and might try to access invalid indices of the array.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <stdio.h> // Determine if two integers are equal without using comparison // and arithmetic operators int checkForEquality(int x, int y) { short arr[x+1]; arr[x] = 0; return (!arr[y]) ? 1 : 0; } int main(void) { int x = 10, y = 10; if (checkForEquality(x, y)) printf ("x=%d is equal to y=%d\n", x, y); else printf ("x=%d is not equal to y=%d\n", x, y); return 0; } |

`Output: `

x=10 is equal to y=10

### 3. Hashing:

Since previous approach consumes a lot of memory, a more space efficient version is to use `std::unordered_map` in C++. Below is C++ implementation:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <iostream> #include <unordered_map> using namespace std; // Determine if two integers are equal without using comparison // and arithmetic operators bool checkForEquality(int x, int y) { unordered_map<int, bool> map; map[x] = 1; return map[y]; } int main(void) { int x = 0, y = 2; if (checkForEquality(x, y)) cout << "x is equal to y"; else cout << "x is not equal to y"; return 0; } |

`Output: `

x is not equal to y

We can also use `std::unordered_set` where we insert the first number in the set and then simply check if second number exists in the set or not.

### 4. Repeated Subtraction:

The idea here is to repeatedly subtract both numbers (say x and y) parallely until one of the numbers becomes 0. If the numbers are equal, then we should be left with `x = 0` and `y = 1`. The solution uses decrement operator (a unary operator) for repeated subtraction.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <stdio.h> // Determine if two integers are equal without using comparison // and arithmetic operators int checkForEquality(int x, int y) { while (--x && --y); return !(x || y-1); } int main(void) { int x = 5, y = 8; if (checkForEquality(x, y)) printf ("x=%d is equal to y=%d\n", x, y); else printf ("x=%d is not equal to y=%d\n", x, y); return 0; } |

`Output: `

x=5 is not equal to y=8

### 5. Simple substraction

If we’re permitted to use arithmetic operators, we can perform simple subtraction to determine if two integers are equal as shown below:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <stdio.h> // Determine if two integers are equal without using comparison operators int checkForEquality(int x, int y) { return !(x - y); } int main(void) { int x = 10, y = 10; if (checkForEquality(x, y)) printf ("x=%d is equal to y=%d\n", x, y); else printf ("x=%d is not equal to y=%d\n", x, y); return 0; } |

`Output: `

x=10 is equal to y=10

**Thanks for reading.**

Please use ideone or C++ Shell or any other online compiler link to post code in comments.

Like us? Please spread the word and help us grow. Happy coding 🙂

## Leave a Reply

In C, one has to initialize stack-allocated arrays before reading from them.