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

1

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);
} 

Output:

Not Same

Source: http://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

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org 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 ide.geeksforgeeks.org, generate link and share the link here.