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

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.

(source: kfupm.edu.sa)

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.

• You've forgotten the "+ 1" after /1100010 ;) Commented Feb 28, 2015 at 1:38

It would be represented in 2's complement. For 4-bit numbers, the most significant bit represents -8 and the remaining 3 bits represent +4, +2, +1 respectively. Therefore the decimal equivalents of all 16 possible numbers are:

1000 = -8+0 = -8; 1001 = -8+1 = -7; 1010 = -8+2 = -6; 1011 = -8+3 = -5; 1100 = -8+4 = -4; 1101 = -8+5 = -3; 1110 = -8+6 = -2; 1111 = -8+7 = -1; 0000 = 0; 0001 = 1; 0010 = 2; 0011 = 3; 0100 = 4; 0101 = 5; 0110 = 6; 0111 = 7;

In your circuit, FA = 'full adder' - see web for truth table.

In 2's complement addition and subtraction, you are not allowed to exceed the range -8 to +7, or an error occurs (that's for 4-bit numbers, of course. For 8-bit numbers the range would be -128 to 127, 16-bit numbers: -2^15 to 2^15-1 = -32768 to 32767 etc...)

So for your 4-bit adder, (0100 - 0111) gives the result 1101 = (-8 +5) = -3.

[0111 is connected to the exclusive-or inputs and 0100 is connected to the FA's]

Note: in 2's complement arithmetic, the carry out of the final full adder is ignored.