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

1 vote
335 views

## Problem:

I wrote a program in python to check if two numbers are equal without using arithmetic and comparison operators. The code I wrote is:

``````def Same(a, b):

while (a & b != 0):

summon = a & b
a = a ^ b
b = summon << 1

return a

if Same(10, 10):
print("Function are same")
else:
print("Function are not same")``````

After running this the output is:

``Function are not same``

Which is wrong.

So, I can not check if two numbers are equal without using arithmetic and comparison operators. Can anyone provide me any code of this.

1 vote

## Solution:

You can try this code to check if two numbers are equal without using arithmetic and comparison operators:

``````def Same(a, b):

# Function to check if two
# numbers are equal using
# XOR operator
if ((a ^ b) != 0):
print("Not Same")
else:
print("Same")

Same(10, 10) ``````

its output will be:

``Same``

You can also try this code:

``````def Same(a, b):
if ( (a & ~b) == 0 and (~a & b) == 0 ):
print("Numbers are Same")
else:
print("Numbers are not Same")

Same(10, 20) ``````

its output will be:

``Numbers are not Same``

Hope this helps you.

Happy coding.

## Related questions

1 vote
29 views
Problem: I tried to add two numbers without using arithmetic operators using python program so I wrote the code: def Add(a, b): # Iterate till there is no summon while (b != 0): # summon now contains common # set bits of a and b summon = a & b # Sum of bits ... <module> print(Add(a, b)) NameError: name 'a' is not defined Why this message is appearing and how to solve this thing? Thanks in advance.
3 views
Problem: Where can I look for a solution to this? > Subtract two numbers without using arithmetic operators .
50 views
26 views
Problem: Why I got this error: how to find maximum and minimum of n numbers without using functions in c++ program any idea on what I need to fix this. I am new in c++ Thanks in advance.
1 vote
81 views
Problem: I wrote a program in python to check if a given number is sparse or not. So I wrote this code: def IsSparse(n): if ((n<<1) & (n>>1)): return 0 return 1 print(IsSparse(35)) print(IsSparse(16)) print(IsSparse(12)) print(IsSparse(72)) And when I run this or ... . The output is: 1 1 1 1 But the actual output should be: 0 1 0 1 What should I do to get the actual result? Thanks in advance.
4 views
Problem: Anyone know how to verify whether two strings are identical in C++?
1 vote
3 views
Problem: Any ideas on what I need to do to fix this? python check if two lists are equal
1 vote