−8 ≤ x[4] ≤ +7
−128 ≤ x[8] ≤ +127
−32768 ≤ x[16] ≤ +32767
−2147483648 ≤ x[32] ≤ +2147483647
What if the result overflows the representation?
If the result of an arithmetic operation is to too large (positive or
negative) to fit into the resultant bit-group, then arithmetic overflow occurs. It is normally left to the programmer to decide how to deal with this situation.
Overflow Rule for addition
If 2 Two's Complement numbers are added, and they both have the same
sign (both positive or both negative), then overflow occurs if and only
if the result has the opposite sign. Overflow never occurs when adding
operands with different signs.
i.e. |
Adding two positive numbers must give a positive result
|
|
Adding two negative numbers must give a negative result
|
Overflow occurs if
- (+A) + (+B) = −C
- (−A) + (−B) = +C
Example: Using 4-bit Two's Complement numbers
(−8 ≤ x ≤ +7)
(−7) 1001
+(−6) 1010
------------
(−13) 1 0011 = 3 : Overflow (largest −ve number is −8)
Overflow Rule for Subtraction
If 2 Two's Complement numbers are subtracted, and their signs are
different, then overflow occurs if and only if the result has the same
sign as the subtrahend.
Overflow occurs if
- (+A) − (−B) = −C
- (−A) − (+B) = +C
Example: Using 4-bit Two's Complement numbers
(−8 ≤ x ≤ +7)
Subtract −6 from +7
(+7) 0111 0111
−(−6) 1010 -> Negate -> +0110
---------- -----
13 1101 = −8 + 5 = −3 : Overflow