A ripple-carry adder

Add two binary numbers of the same length with a chain of full adders. Print the sum (same length), the final carry, and whether the addition overflowed as signed two's complement numbers.

Input

Two lines, each a binary number of 1 to 32 bits. Both have the same length.

Output

Three lines: the sum bits, carry and the carry out of the leftmost bit, and overflow yes or overflow no.

Examples

Example 1 · Input
0111
0101
Expected output
1100
carry 0
overflow yes
Example 2 · Input
1111
0001
Expected output
0000
carry 1
overflow no

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Go from the rightmost bit to the left with a carry starting at 0.

Hint 2

sum = a ^ b ^ carry; carry = (a & b) | (carry & (a ^ b)).

Hint 3

Signed overflow: both inputs have the same top bit and the sum's top bit is different.

Solution

Show one correct solution (try it yourself first)
a = input().strip()
b = input().strip()
carry, out = 0, ""
for x, y in zip(reversed(a), reversed(b)):
    x, y = int(x), int(y)
    out = str(x ^ y ^ carry) + out
    carry = (x & y) | (carry & (x ^ y))
print(out)
print("carry", carry)
print("overflow", "yes" if a[0] == b[0] and out[0] != a[0] else "no")

Lesson: Adders: from half adder to ripple carry →