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

Following are not allowed to use
1) Arithmetic and Comparison Operators
2) String functions

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Method 1 : The idea is to use XOR operator. XOR of two numbers is 0 if the numbers are same, otherwise non-zero.

C++

 `// C++ program to check if two numbers ` `// are equal without using arithmetic ` `// and comparison operators ` `#include ` `using` `namespace` `std; ` ` `  `// Function to check if two ` `// numbers are equal using  ` `// XOR operator ` `void` `areSame(``int` `a, ``int` `b) ` `{ ` `   ``if` `(a^b)  ` `   ``cout << ``"Not Same"``;  ` `   ``else`  `   ``cout << ``"Same"``; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{  ` `     `  `    ``// Calling function ` `    ``areSame(10, 20); ` `}  `

Java

 `// Java program to check if two numbers ` `// are equal without using arithmetic ` `// and comparison operators ` `class` `GFG { ` ` `  `// Function to check if two ` `// numbers are equal using  ` `// XOR operator ` `static` `void` `areSame(``int` `a, ``int` `b) ` `{ ` `    ``if` `((a ^ b) != ``0``)  ` `    ``System.out.print(``"Not Same"``);  ` `    ``else`  `        ``System.out.print(``"Same"``); ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{  ` `     `  `    ``// Calling function ` `    ``areSame(``10``, ``20``); ` `}  ` `} ` ` `  `// This code is contributed by Smitha `

Python 3

 `# Python3 program to check if two numbers ` `# are equal without using arithmetic ` `# and comparison operators ` ` `  `def` `areSame(a, b): ` ` `  `# Function to check if two ` `# numbers are equal using  ` `# XOR operator ` ` ``if` `((a ^ b) !``=` `0``): ` `    ``print``(``"Not Same"``)  ` ` ``else``: ` `    ``print``(``"Same"``) ` ` `  `# Driver Code ` ` `  `areSame(``10``, ``20``) ` ` `  `# This code is contributed by Smitha `

C#

 `// C# program to check if two numbers ` `// are equal without using arithmetic ` `// and comparison operators ` `using` `System; ` ` `  `class` `GFG { ` ` `  `// Function to check if two ` `// numbers are equal using  ` `// XOR operator ` `static` `void` `areSame(``int` `a, ``int` `b) ` `{ ` `    ``if` `((a ^ b) != 0)  ` `    ``Console.Write(``"Not Same"``);  ` `    ``else` `    ``Console.Write(``"Same"``); ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{  ` `     `  `    ``// Calling function ` `    ``areSame(10, 20); ` `}  ` `} ` ` `  `// This code is contributed by Smitha `

PHP

 ` `

Output :

`Not Same`

Method 2 : Here idea is using complement ( ~ ) and bit-wise ‘&’ operator.

C++

 `// C++ program to check if two numbers ` `// are equal without using arithmetic ` `// and comparison operators ` `#include ` `using` `namespace` `std; ` ` `  `// Function to check if two ` `// numbers are equal using  ` `// using ~ complement and & operator. ` `void` `areSame(``int` `a, ``int` `b) ` `{ ` `    ``if` `( (a & ~b) )  ` `    ``cout << ``"Not Same"``;  ` `    ``else` `    ``cout << ``"Same"``; ` `} ` `// Driver Code ` `int` `main() ` `{  ` `     `  `    ``// Calling function ` `    ``areSame(10, 20); ` `}  `

Java

 `// Java program to check if two numbers ` `// are equal without using arithmetic ` `// and comparison operators ` ` `  `class` `GFG ` `{ ` `// Function to check if two ` `// numbers are equal using  ` `// using ~ complement and & operator. ` `static` `void` `areSame(``int` `a, ``int` `b) ` `{ ` `    ``if` `( (a & ~b) != ``0` `)  ` `        ``System.out.print(``"Not Same"``);  ` `    ``else` `        ``System.out.print(``"Same"``); ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{  ` `    ``// Calling function ` `    ``areSame(``10``, ``20``); ` `}  ` `} ` ` `  `// This code is contributed ` `// by Akanksha Rai `

Python3

 `# Python3 program to check if two numbers  ` `# are equal without using arithmetic  ` `# and comparison operators  ` ` `  `# Function to check if two  ` `# numbers are equal using  ` `# using ~ complement and & operator.  ` `def` `areSame(a, b): ` `    ``if` `( (a & ~b) ):  ` `        ``print``(``"Not Same"``)  ` `    ``else``: ` `        ``print``(``"Same"``)      ` `# Calling function  ` `areSame(``10``, ``20``) ` ` `  `#This code is contributed by Rajput-Ji `

C#

 `// C# program to check if two numbers ` `// are equal without using arithmetic ` `// and comparison operators ` `using` `System; ` ` `  `class` `GFG ` `{ ` `// Function to check if two ` `// numbers are equal using  ` `// using ~ complement and & operator. ` `static` `void` `areSame(``int` `a, ``int` `b) ` `{ ` `    ``if` `( (a & ~b) != 0 )  ` `        ``Console.Write(``"Not Same"``);  ` `    ``else` `        ``Console.Write(``"Same"``); ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main() ` `{  ` `    ``// Calling function ` `    ``areSame(10, 20); ` `}  ` `} ` ` `  `// This code is contributed ` `// by Akanksha Rai `

PHP

 ` `

Output :

```Not Same
```