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

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

#include <iostream>
using namespace std;

void areSame(int a, int b)
   if (a^b) cout << "Not Same"; 
   else cout << "Same";

int main()
   areSame(10, 20);


Not Same


Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

GATE CS Corner    Company Wise Coding Practice

Please write to us at to report any issue with the above content.

Recommended Posts:

1 Average Difficulty : 1/5.0
Based on 6 vote(s)

Writing code in comment? Please use, generate link and share the link here.