Given two numbers, the task is to check if two numbers are equal without using Arithmetic and Comparison Operators or String functions.
Method 1 : The idea is to use XOR operator. XOR of two numbers is 0 if the numbers are the same, otherwise non-zero.
C++
| // C++ program to check if two numbers// are equal without using arithmetic// and comparison operators#include <iostream>usingnamespacestd;// Function to check if two// numbers are equal using// XOR operatorvoidareSame(inta, intb){    if(a ^ b)        cout << "Not Same";    else        cout << "Same";}// Driver Codeintmain(){    // Calling function    areSame(10, 20);} | 
Java
| // Java program to check if two numbers// are equal without using arithmetic// and comparison operatorsclassGFG {    // Function to check if two    // numbers are equal using    // XOR operator    staticvoidareSame(inta, intb)    {        if((a ^ b) != 0)            System.out.print("Not Same");        else            System.out.print("Same");    }    // Driver Code    publicstaticvoidmain(String[] args)    {        // Calling function        areSame(10, 20);    }}// This code is contributed by Smitha | 
Python3
| # Python3 program to check if two numbers# are equal without using arithmetic# and comparison operatorsdefareSame(a, b):# Function to check if two# numbers are equal using# XOR operator if((a ^ b) !=0):    print("Not Same") else:    print("Same")# Driver CodeareSame(10, 20)# This code is contributed by Smitha | 
C#
| // C# program to check if two numbers// are equal without using arithmetic// and comparison operatorsusingSystem;classGFG {    // Function to check if two    // numbers are equal using    // XOR operator    staticvoidareSame(inta, intb)    {        if((a ^ b) != 0)            Console.Write("Not Same");        else            Console.Write("Same");    }    // Driver Code    publicstaticvoidMain(String[] args)    {        // Calling function        areSame(10, 20);    }}// This code is contributed by Smitha | 
PHP
| <?php// PHP program to check if// two numbers are equal// without using arithmetic// and comparison operators// Function to check if two// numbers are equal using// XOR operatorfunctionareSame($a, $b){if($a^ $b)echo"Not Same";elseecho"Same";}// Driver Code// Calling functionareSame(10, 20);// This code is contributed// by nitin mittal.?> | 
Javascript
| <script>// Javascript program to check if two numbers// are equal without using arithmetic and// comparison operators  // Function to check if two// numbers are equal using// XOR operatorfunctionareSame(a, b){    if((a ^ b) != 0)        document.write("Not Same");    else        document.write("Same");}// Driver CodeareSame(10, 20);// This code is contributed by shikhasingrajput</script> | 
Not Same
Time Complexity: O(1)
Auxiliary Space: O(1)
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 <iostream>usingnamespacestd;// Function to check if two// numbers are equal using// using ~ complement and & operator.voidareSame(inta, intb){    if((a & ~b) == 0)        cout << "Same";    else        cout << "Not Same";}// Driver Codeintmain(){    // Calling function    areSame(10, 20);    // This Code is improved by Sonu Kumar Pandit} | 
Java
| // Java program to check if two numbers// are equal without using arithmetic// and comparison operatorsclassGFG {    // Function to check if two    // numbers are equal using    // using ~ complement and & operator.    staticvoidareSame(inta, intb)    {        if((a & ~b) == 0&& (~a & b) == 0)            System.out.print("Same");        else            System.out.print("Not Same");    }    // Driver Code    publicstaticvoidmain(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.defareSame(a, b):    if((a & ~b) ==0and(~a & b) ==0):        print("Same")    else:        print("Not Same")# Calling functionareSame(10, 20)# This code is contributed by Rajput-Ji | 
C#
| // C# program to check if two numbers// are equal without using arithmetic// and comparison operatorsusingSystem;classGFG {    // Function to check if two    // numbers are equal using    // using ~ complement and & operator.    staticvoidareSame(inta, intb)    {        if((a & ~b) == 0 && (~a & b) == 0)            Console.Write("Same");        else            Console.Write("Not Same");    }    // Driver Code    publicstaticvoidMain()    {        // Calling function        areSame(10, 20);    }}// This code is contributed// by Akanksha Rai | 
PHP
| <?php// PHP 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.functionareSame($a, $b){    if(($a& ~$b)==0 && (~$a& $b)==0)        echo"Same";    else        echo"Not Same";}// Driver Code// Calling functionareSame(10, 20);// This code is contributed by ita_c?> | 
Javascript
| <script>// Javascript 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.functionareSame(a, b){    if((a & ~b) == 0 && (~a & b) == 0)        document.write("Same");    else        document.write("Not Same");}// Driver Code// Calling functionareSame(10, 20);// This code is contributed by gauravrajput1</script> | 
Not Same
Time Complexity: O(1)
Auxiliary Space: O(1)
 
Using bit manipulation:
Approach:
Another approach is to use bit manipulation to compare each bit of the two numbers. We can use the bit-shift operators to extract each bit and compare them one by one.
- Define a function named is_equal that takes two arguments num1 and num2.
- Initialize a variable mask to 1.
- Loop through the range of 32 bits (assuming 32-bit integers).
- Use the bitwise AND operator (&) to extract the i-th bit of num1 and num2.
- Compare the extracted bits using the not equal to operator (!=).
- If the extracted bits are not equal, return False.
- Shift the mask left by one bit using the left shift operator (<<).
- Return True if all bits are equal.
Python3
| defis_equal(num1, num2):    mask =1    fori inrange(32):  # assuming 32-bit integers        if(num1 & mask) !=(num2 & mask):            returnFalse        mask <<=1    returnTrue# Example usageprint(is_equal(10, 10))  # Output: Trueprint(is_equal(10, 20))  # Output: False | 
True False
Time complexity: O(log n)
Space complexity: O(1)
Source: https://www.geeksforgeeks.org/count-of-n-digit-numbers-whose-sum-of-digits-equals-to-given-sum/
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above


 
                                    







