Addition and Subtraction With Signed 2s Complement
Addition and Subtraction With Signed 2s Complement
Overflow
• When two numbers of n digits each are added and the sum occupies n + 1 digits, we say
that an overflow occurred.
• An overflow is a problem in digital computers because the width of registers is finite.
• A result that contains n + 1 bits cannot be accommodated in a register with a standard
length of n bits.
• For this reason, many computers detect the occurrence of an overflow, by setting a flip-
flop which can then be checked by the user.
• When two unsigned numbers are added, an overflow is detected from the end carry out
of the most significant position.
• In the case of signed numbers, the leftmost bit always represents the sign.
• When two signed numbers are added, the sign bit is treated as part of the number and
the end carry does not indicate an overflow.
• An overflow may occur only if the two numbers added are both positive or both
negative.
• Example: Two signed binary numbers, + 70 and + 80, are stored in two 8-bit registers
• Each register can accommodate values from binary + 127 to binary - I28.
• Since the sum of the two numbers is + I50, it exceeds the capacity of the 8-bit register.
• 8-bit result that should have been positive has a negative sign bit and the 8-bit result
that should have been negative has a positive sign bit.
• If, however, the carry out of the sign bit position is taken as the sign bit of the result, the
9-bit answer so obtained will be correct.
• Since the answer cannot be accommodated within 8 bits, we say that an overflow
occurred.
• An overflow can be detected by applying the last two carries out of the addition in the
above example to an exclusive-OR gate.
• The overflow is detected when the output of the gate is equal to 1.
Hardware Implementation:
• The hardware implementation for signed 2's complement addition and subtraction is
shown in the below figure.
• Let AC(accumulator) and BR be two registers that hold the two numbers for addition
or subtraction.
• The leftmost bit in AC and BR represent the sign bits of the numbers.
• The two sign bits are added or subtracted together with the other bits in the
complementer and parallel adder.
• The sum is obtained by adding the contents of AC and BR (including their sign bits).
• The overflow bit V is set to 1 if the exclusive-OR of the last two carries is 1, and it
is cleared to 0 otherwise.
• If an overflow occurs, there will be an erroneous result in the AC register
• The subtraction operation is accomplished by adding the content of AC to the 2's
complement of BR.
• The algorithm for adding and subtracting two binary numbers in signed-2' s
complement representation is shown in the following flowchart.