Logic 1:
If two numbers are equal the bitwise XOR result will be zero. In this way we can check two numbers are equal without any comparison.
For example :
1) 5^5 = 0
(5)10 = (0101)2
0101
0101
-------
0000
2) 12^12 = 0
(12)10 = (1100)2
1100
1100
------
0000
C program :
int main() { int num1 = 10, num2 = 10; if((num1 ^ num2) == 0) printf("Equal"); else printf("Unequal"); return 0; }
Logic 2 :
We know, subtraction of two equal numbers is Zero. Means number are zero if equal other wise non-zero.
C program :int main() { int num1 = 10, num2 = 10; if(!(num1 - num2)) printf("Equal"); else printf("Unequal"); return 0; }
No comments:
Post a Comment