What would be the output of A = 0100 and B = 0111 with S = 1 in a 4-bit binary adder-subtractor

adderbinarydigital-logiclogic-gates

I understand S = 1 means subtraction and S = 0 means addition. So then, the output would be the result of 0100 – 0111. But since it's negative, I'm not sure how it would be represented. I know the answer is -3, or -11 in binary, but what would be the output in the adder-subtractor?

I think what's getting me is what goes on in the circuit in the blocks labeled "FA", and what happens with the final carry bit (if it's 1 of course). I do understand how to do binary addition and subtraction.

adder-subtractor
(source: kfupm.edu.sa)

Best Answer

The blocks labeled "FA" are called full adders, and they are fundamental to how binary arithmetic is done at the gate level. The circuit you have presented here is an implementation of a 4-bit adder/subtractor. Combining addition and subtraction in the same operation requires the use of an alternative representation of binary numbers. This is called 2's complement.

It turns out that doing the operation $$A - B = C$$ is the same as doing the operation $$\overline{A} + 1 + B = C$$ The A with a line over it means we have taken the complement of A, which simply means we have swapped 0's with 1's and 1's with 0's. Example: $$\overline{1100010} + 1 = 0011101 + 0000001 = 0011110$$ In the circuit you provided, the XOR-gates perform the function of flipping the input bits A if S = 1. The input S also provide an additional carry bit into the first full adder, which represents adding 1 to the number. The short answer to your question is this: $$ A = 0100, B = 0111$$ $$A - B = 0100 - 0111 = 0100 + (\overline{0111} + 1) = 0100 + 1001 = 1101$$

Our result looks fishy, but note that when we put a 2's complement in, we'll get one out. This is the digital representation of the negative number, and by taking the complement again, we can more easily see the actual value. $$\overline{1101} = 0010 + 0001 = 0011$$ Which matches your prediction.

If you're looking to understand digital logic and binary arithmetic, I would advise you to not think of S = 1 as simply meaning subtraction. In practice, the function of S is to determine whether or not a subtraction should take place, but it won't give any insight into why it does that. Look up a cuircuit diagram of a single full adder, understand how it works and then go back the problem of a ripple carry adder/subtractor.