Check if two numbers are equal using bitwise operators
Let's write a c program to check if two numbers are equal or not using bitwise operators.
Logic
If two numbers are equal the bitwise XOR (^) result will be zero.
Bitwise XOR OperatorExample
2 ^ 2
0010
0010
------
0000
Example
5 ^ 5
0101
0101
------
0000
Binary equivalent of 2 is 0010.
Binary equivalent of 5 is 0101.
Program
Example
/* * Program : Check if two numbers are equal using bitwise operators * Language : C */ #include<stdio.h> int main() { int num1,num2; scanf("%d%d",&num1,&num2); if((num1 ^ num2) == 0) printf("Equal\n"); else printf("Unequal\n"); return 0; }