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

18 Jan, 2022
Following are not allowed to use 1) Arithmetic and Comparison Operators 2) String functions
Method 1 : The idea is to use XOR operator. XOR of two numbers is 0 if the numbers are same, otherwise non-zero. 
C++
Java
Python3
C#
PHP
Javascript
// C++ program to check if two numbers// are equal without using arithmetic// and comparison operators#include <iostream>using namespace std; // Function to check if two// numbers are equal using// XOR operatorvoid areSame(int a, int b){    if (a ^ b)        cout << "Not Same";    else        cout << "Same";} // Driver Codeint main(){     // Calling function    areSame(10, 20);}
// Java program to check if two numbers// are equal without using arithmetic// and comparison operatorsclass 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
# 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# program to check if two numbers// are equal without using arithmetic// and comparison operatorsusing 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// 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 operatorfunction areSame($a, $b){if ($a ^ $b)echo "Not Same";elseecho "Same";} // Driver Code // Calling functionareSame(10, 20); // This code is contributed// by nitin mittal.?>
<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 operatorfunction areSame(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>
Output : 
Not Same
Time Complexity: O(1)
Auxiliary Space: O(1)
Method 2 : Here idea is using complement ( ~ ) and bit-wise ‘&’ operator. 
C++
Java
Python3
C#
PHP
Javascript
// C++ program to check if two numbers// are equal without using arithmetic// and comparison operators#include <iostream>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) == 0)        cout << "Same";    else        cout << "Not Same";}// Driver Codeint main(){     // Calling function    areSame(10, 10);     // This Code is improved by Sonu Kumar Pandit}
// 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 && (~a & b) == 0)            System.out.print("Same");        else            System.out.print("Not Same");    }     // Driver Code    public static void main(String args[])    {        // Calling function        areSame(10, 20);    }} // This code is contributed// by Akanksha Rai
# 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) == 0 and (~a & b) == 0):        print("Same")    else:        print("Not Same")  # Calling functionareSame(10, 20) # This code is contributed by Rajput-Ji
// C# program to check if two numbers// are equal without using arithmetic// and comparison operatorsusing 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 && (~a & b) == 0)            Console.Write("Same");        else            Console.Write("Not Same");    }     // Driver Code    public static void Main()    {        // Calling function        areSame(10, 20);    }} // This code is contributed// by Akanksha Rai
<?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.function areSame($a, $b){    if (($a & ~$b)==0 && (~$a & $b)==0)        echo "Same";    else        echo "Not Same";} // Driver Code// Calling functionareSame(1, 1); // This code is contributed by ita_c?>
<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.function areSame(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>
Output : 
Not Same
Time Complexity: O(1)
Auxiliary Space: O(1)
YouTubeGeeksforGeeks502K subscribersCheck if two numbers are equal without using comparison operators | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 2:16•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=if_uoOTVgXw" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
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 
Smitha Dinesh Semwal
nitin mittal
vivek kumar 9
Rajput-Ji
ukasp
Akanksha_Rai
competing23invain
shikhasingrajput
GauravRajput1
subham348
sonukmrpndt
Bit Magic
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Cyclic Redundancy Check and Modulo-2 Division
Little and Big Endian Mystery
Program to find whether a given number is power of 2
Binary representation of a given number
Find the element that appears once
Add two numbers without using arithmetic operators
Bit Fields in C
Josephus problem | Set 1 (A O(n) Solution)
Set, Clear and Toggle a given bit of a number in C
C++ bitset and its application