Compare two integers without using any Comparison operator - GeeksforGeeks

06 Sep, 2021
Given two integers A & B. Task is to check if A and B are same or not without using comparison operators.Examples:  
Input : A = 5 , B = 6
Output : 0

Input : A = 5 , B = 5 
Output : 1

Note : 1 = "YES" and 0 = "NO"
 
idea is pretty simple we do Xor of both element ( A , B ) . if Xor is zero then two number are equal else not . Below is the implementation of above idea :  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to compare two integers without// any comparison operator.#include<bits/stdc++.h>using namespace std; // function return true if A ^ B > 0  else falsebool EqualNumber(int A, int B){   return ( A ^ B ) ;} // Driver programint main(){  int A = 5 , B = 6;  cout << !EqualNumber(A, B) << endl;  return 0;}
// Java program to compare two integers without// any comparison operator.import java.util.*; class solution{ // function return true if A ^ B > 0 else falsestatic boolean EqualNumber(int A, int B){       if ((A^B) != 0)   return true;  else   return false;} // Driver programpublic static void main(String args[]){int A = 5 , B = 6;if(EqualNumber(A, B) == false) System.out.println(1);else System.out.println(0); }}// This code is contributed by// Surendra_Gangwar
# Python3 program to compare two integers# without any comparison operator. # Function return true if# A ^ B > 0 else falsedef EqualNumber(A, B):     return ( A ^ B ) # Driver CodeA = 5; B = 6print(int(not(EqualNumber(A, B)))) # This code is contributed by Smitha Dinesh Semwal.
// C# program to compare two integers// without any comparison operator.using System; class GFG{// function return true if// A ^ B > 0 else falsestatic bool EqualNumber(int A, int B){    if(( A ^ B ) > 0)        return true;    else        return false;} // Driver Codepublic static void Main(){    int A = 5 , B = 6;    if(!EqualNumber(A, B) == false)        Console.WriteLine("0");    else        Console.WriteLine("1");}} // This code is contributed// by Akanksha Rai
<?php// PHP program to compare two integers// without any comparison operator. // function return true if// A ^ B > 0 else falsefunction EqualNumber($A, $B){return ( $A ^ $B ) ;} // Driver Code$A = 5 ;$B = 6;echo ((int)!(EqualNumber($A, $B))) . "\n"; // This code is contributed// by ChitraNayal?>
<script> // JavaScript program to compare two integers without// any comparison operator. // function return true if A ^ B > 0 else falsefunction EqualNumber(A, B){return ( A ^ B ) ;} // Driver program let A = 5 , B = 6; if(!EqualNumber(A, B) == false)    document.write("0"); else    document.write("1");    // This code is contributed by Surbhi Tyagi. </script>
Output: 
0
Reference :http://stackoverflow.com/questions/476800/comparing-two-integers-without-any-comparisonThis article is contributed by Nishant Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
ukasp
Akanksha_Rai
SURENDRA_GANGWAR
surbhityagi15
sumitgumber28
Bitwise-XOR
Bit Magic
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Set, Clear and Toggle a given bit of a number in C
Check whether bitwise AND of a number with any subset of an array is zero or not
Find the size of Largest Subset with positive Bitwise AND
Write an Efficient Method to Check if a Number is Multiple of 3
Highest power of 2 less than or equal to given number
Check for Integer Overflow
Swap two nibbles in a byte
Swap bits in a given number
Reverse actual bits of the given number
Bit Tricks for Competitive Programming