Skip to content
Related Articles

Related Articles

Improve Article
Compare two integers without using any Comparison operator
  • Difficulty Level : Basic
  • Last Updated : 04 Mar, 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++




// C++ program to compare two integers witout
// any comparison operator.
#include<bits/stdc++.h>
using namespace std;
 
// function return true if A ^ B > 0  else false
bool EqualNumber(int A, int B)
{
   return ( A ^ B ) ;
}
 
// Driver program
int main()
{
  int A = 5 , B = 6;
  cout << !EqualNumber(A, B) << endl;
  return 0;
}

Java




// Java program to compare two integers witout
// any comparison operator.
import java.util.*;
 
class solution
{
 
// function return true if A ^ B > 0 else false
static boolean EqualNumber(int A, int B)
{
     
  if ((A^B) != 0)
   return true;
  else
   return false;
}
 
// Driver program
public 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




# Python3 program to compare two integers
# without any comparison operator.
 
# Function return true if
# A ^ B > 0 else false
def EqualNumber(A, B):
 
    return ( A ^ B )
 
# Driver Code
A = 5; B = 6
print(int(not(EqualNumber(A, B))))
 
# This code is contributed by Smitha Dinesh Semwal.

C#




// C# program to compare two integers
// without any comparison operator.
using System;
 
class GFG
{
// function return true if
// A ^ B > 0 else false
static bool EqualNumber(int A, int B)
{
    if(( A ^ B ) > 0)
        return true;
    else
        return false;
}
 
// Driver Code
public 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
// PHP program to compare two integers
// without any comparison operator.
 
// function return true if
// A ^ B > 0 else false
function EqualNumber($A, $B)
{
return ( $A ^ $B ) ;
}
 
// Driver Code
$A = 5 ;
$B = 6;
echo ((int)!(EqualNumber($A, $B))) . "\n";
 
// This code is contributed
// by ChitraNayal
?>

Javascript




<script>
 
// JavaScript program to compare two integers witout
// any comparison operator.
 
// function return true if A ^ B > 0 else false
function 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-comparison
This article is contributed by Nishant Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@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.
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes




My Personal Notes arrow_drop_up
Recommended Articles
Page :