Adders & Subtractors — worked example

Digital Logic 101 · Adders & Subtractors · Example

Example 1: (+5) + (−3) in 4-bit two's complement

  1. Encode the operands. Positive values are their plain binary patterns; negate by inverting and adding 1.
    +510=01012310:  0011+1=1100+1=11012+5_{10} = 0101_2 \quad -3_{10}: \;\sim 0011 + 1 = 1100 + 1 = 1101_2
  2. Column-add LSB to MSB. Treat the bits as unsigned for the add; the two's-complement interpretation comes back at the end.
    1110  (carries)0101+110110010\begin{array}{rrrrrl}& 1 & 1 & 1 & 0 & \;\text{(carries)} \\ & 0 & 1 & 0 & 1 \\ + & 1 & 1 & 0 & 1 \\ \hline \\ 1 & 0 & 0 & 1 & 0\end{array}
    The bit that falls off the MSB is the carry-out.
  3. Read the flags. The 4-bit result is 00102=+2100010_2 = +2_{10}. Carry-out = 11 (a bit fell off the top).
  4. Overflow check. Operand signs are +,+, - — they disagree, so overflow is impossible. Equivalently, the carry into the MSB column is 11 and the carry out is 11; V=CinCout=0V = C_{in} \oplus C_{out} = 0.

Example 2: (+7) + (+1) in 4-bit two's complement

  1. Encode the operands. Both positive, both already in binary.
    +710=01112+110=00012+7_{10} = 0111_2 \quad +1_{10} = 0001_2
  2. Column-add.
    1110  (carries)0111+000101000\begin{array}{rrrrrl}& 1 & 1 & 1 & 0 & \;\text{(carries)} \\ & 0 & 1 & 1 & 1 \\ + & 0 & 0 & 0 & 1 \\ \hline \\ 0 & 1 & 0 & 0 & 0\end{array}
  3. Read the flags. 4-bit result is 100021000_2. As an unsigned value that's +8+8, but as 4-bit signed it's 8-8. Carry-out = 00.
  4. Overflow check. Operand signs are +,++, + but the result sign is -. Signed overflow. Carry into MSB = 11; carry out of MSB = 00; V=10=1V = 1 \oplus 0 = 1.
In 4-bit signed arithmetic, the legal result range is 8-8..+7+7. The true sum +8+8 doesn't fit, so the bits land at 8-8 — wrapping past the top of the positive range and into the negative range. That's exactly what the overflow flag is reporting.

Try other operands in the Simulate stage — toggle the bits, flip the subtract switch, and watch the flags update live.