+3

# Add Numbers without using "+"

Hi I have to write a program that, for example takes an input, like this: 2 + 4 + 20 + 4+.... and displays the binary equivalent of their sum in the output. and I'm not allowed to use + to add Numbers in the program. I can only use + in the input itself. can anyone give me an idea to solve?

4/29/2021 10:12:52 AM

Marjaf

+9

You can look at each bits in a loop untill either operand << value is 0. for two operands A and B. 1. create a local variable Z that hold the carry out from each bits of A and B z = A & B 2. make A the result of the XOR (A ^ B) this could be addition without carry A = A ^ B 3. make B the result of carryIn for each bit such that B = Z << 1 def add(x, y): while y: z = x & y x = x ^ y y = z << 1 return x The loop keeps adding the carry until the carry is 0 for all bits. print(add(-3, -2)). // -5 print(add(3, 3)) // 6 However, there's a drawback in this method when either of the operand is "unsigned" and that would result to an undefined behavior. print(add(3, -2)) //undefined Well this make senses due to the way the left-shift operator is handled in Python. Instead of reaching an upper limit on the integer bits, and setting the highest bit to make a number negative, it becomes positive long integers.

+6

print(bin(eval(input()))[2:])

+4

+1

It should be quite easy to sum up binary decimals without + operator if you just can convert the numbers to binary.

+1

U can use either concatenation r else use append

0

(,) concatenation can used

0

CarrieForle no sum, -(-) ,... is not allowed tooπ

0

Mirielle Frogged Rohith Paul Mathew Seb TheS Thank you so much π

0

You can tickmark the answer you liked as the solution. That'll help people looking for the same answer.