• Register
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.

Thanks in advance.

8 6 2
4,980 points

Please log in or register to answer this question.

1 Answer

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.

11 6 4
34,950 points

Related questions

1 vote
1 answer 29 views
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.
asked Jun 11, 2020 samhaz 5k points
0 votes
1 answer 3 views
3 views
Problem: Where can I look for a solution to this? > Subtract two numbers without using arithmetic operators .
asked 5 days ago Isac Christiaan 9.6k points
0 votes
1 answer 26 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.
asked Mar 11 jasimchaudary 1.5k points
1 vote
1 answer 81 views
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.
asked Apr 22, 2020 samhaz 5k points
0 votes
1 answer 4 views
4 views
Problem: Anyone know how to verify whether two strings are identical in C++?
asked Apr 17 jamuna1 30.2k points
1 vote
1 answer 3 views
3 views
Problem: Any ideas on what I need to do to fix this? python check if two lists are equal
asked Mar 30 MUHAMMAD MUNEEB 83k points
1 vote
1 answer 4 views
4 views
Problem: Can someone please help fix this uncaught error : python check if two strings are equal
asked Mar 30 MUHAMMAD MUNEEB 83k points
0 votes
1 answer 7 views
7 views
Problem: matlab check if two matrices are equal.
asked Mar 21 charles mathews 5.5k points
0 votes
1 answer 9 views
9 views
Problem: anyone who know about this problem please help? python how to check if two strings are equal
asked Mar 18 Rohit kr 17.5k points