A full adder adds three bits: a, b and the carry in cin. It gives a sum bit and a carry out.
What to build
Print the sum bit and the carry out of a full adder.
Requirements
- Use only gate operations on the bits (XOR, AND, OR), as a real full adder does.
- Print the sum first, then the carry out.
Input
One line with three bits a, b and cin.
Output
One line with the sum bit and the carry out, separated by a space.
Constraints
Each input is 0 or 1.
Example
Input:
1 1 0
Output:
0 1
Why it works
Adding three bits gives a number from 0 to 3, which needs two bits: the low bit is the sum and the high bit is the carry. The sum is 1 when an odd number of the inputs are 1 (XOR of all three), and the carry is 1 when at least two of the inputs are 1.
Examples
1 1 0
0 1
1 1 1
1 1
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
sum = a ^ b ^ cin.
Hint 2
carry = (a & b) | (cin & (a ^ b)): a carry when both bits are 1, or when exactly one is 1 and a carry came in.
Hint 3
The pair (carry, sum) as a number is a + b + cin.
Solution
Show one correct solution (try it yourself first)
a, b, cin = map(int, input().split()) s = a ^ b ^ cin cout = (a & b) | (cin & (a ^ b)) print(s, cout)
▶ Open this solution in the compiler, then press Visualize to watch it run step by step