In the case of integer numbers, the addition and subtraction algorithms depend on the particular representation. Three nonredundant representation methods are considered in what follows: B's complement, sign-magnitude, and excess-E (Chapter 3).
Given two n-digit B's complement integers x and y, and an initial carry cin equal to 0 or 1, then z = x + y + cin is an (n + 1)-digit B's complement integer. Assume that x and y are represented with n+1 digits. Then the natural numbers associated with x, y, and z are R(x) = x mod Bn+1, R(y) = y mod Bn+1, and R(z) = z mod Bn+1 (Definition 3.4), so that
Thus a straightforward addition algorithm consists in representing x and y with n+1 digits and adding the corresponding natural numbers, as well as the initial carry, modulo Bn+1 (that means without taking into account the output carry). In order to represent x and y with one additional digit, Comment 3.2 is taken into account. As before, the procedure natural_addition computes the sum of two natural numbers.
Algorithm 4.18 B's Complement Addition
if x(n-1)<B/2 then x(n):=0; else x(n):=B-1; end if; if y(n-1)<B/2 then y(n):=0; else y(n):=B-1; end if; natural_addition(n+1, c_in, x, y, not_used, z);
Example 4.1 Assume that B = 10, n = 4, cin = 0, x = −2345, and y = −3674.. Both x and y are negative so that they are represented by R(x) = − 2345 + 104 = 7655 and R(y) = −3674 + 104 = 6326. First represent x and y with five digits: R(x) = 97655 and R(y) = 96326. Then add up R(x) and R(y) modulo 105: (97655 + 96326) mod 105 = 93981. As 9 ≥ B/2 = 5, the integer represented by 93981 is negative and equal to 93981 − 105 = − 6019, that is, the sum of x and y.
Given an n-digit B's complement integer x, the inverse z = −x of x is an (n + 1)-digit B's complement integer (actually the only case when − x cannot be represented with n digits is when x = −Bn/2 and − x = Bn/2; i.e., − x = − 0.Bn + (B/2). Bn−1 + 0.Bn−2 +…+ 0.B0). The computation of the representation of −x is based on the following property.
Property 4.3 Given two m-digit base-B natural numbers a = am−1Bm−1 + am−2. Bm−2 +…+ a0. B0 and b = (B − 1 − am−1). Bm−1 + (B − 1 − am−2).Bm−2 + … + (B − 1 − a0).B0, then
Assume that x is represented with n + 1 digits, and define x′ as being the natural number deduced from R(x) by substituting every digit xi by = B − 1 − xi. Then, according to Property 4.3,
and
A straightforward inversion algorithm consists in representing x with n + 1 digits, complementing every digit to B − 1, then adding 1.
Algorithm 4.19 B's Complement Sign Change
if x(n-1)<B/2 then x(n):=0; else x(n):=B-1; end if; for i in 0..n loop x′(i):=B-1-y(i); end loop; natural_addition(n+1, 1, x′, 0, not_used, z);
Examples 4.2
An alternative sign-change algorithm is based on the following observation: if x is represented under the form
where xk > 0, then the representation of − x is
In the following algorithm, the binary variable first_non_zero, initially equal to 0, is set to 1 as soon as the first nonzero digit of x is encountered.
Algorithm 4.20 B's Complement Sign Change (Alternative Algorithm)
if x(n-1)<B/2 then x(n):=0; else x(n):=B-1; end if; first_non_zero:=0; for i in 0..n loop if first_non_zero=0 then if x(i)=0 then z(i):=0; else z(i):=B-x(i); first_non_zero :=1; end if; else z(i):=B-1-x(i); end if; end loop;
Given two n-digit B's complement integers x and y, and an input borrow bin equal to 0 or 1, then z = x − y − bin is an (n + 1)-digit B's complement integer. Assume that x and y are represented with n + 1 digits. Then the natural numbers associated with x, − y, and z are R(x) = x mod Bn+1, R(−y) = (y′ + 1) mod Bn+1 (relation (4.15)) and R(z) = z mod Bn+1, so that
Thus a straightforward subtraction algorithm consists in representing x and y with n + 1 digits, complementing the digits of y, and adding, modulo Bn+1, the corresponding natural numbers, as well as the inverted input borrow.
Algorithm 4.21 B's Complement Subtraction
if x(n-1)<B/2 then x(n):=0; else x(n):=B-1; end if; if y(n-1)<B/2 then y(n):=0; else y(n):=B-1; end if; for i in 0..n loop y′(i):=B-1-y(i); end loop; c_in:=1-b_in; natural_addition(n+1, x, y′, c_in, z, not_used);
Example 4.3 Assume that B = 10, n = 4, bin = 1, x = − 2345, and y = 3674; x is negative and y nonnegative, so that they are represented by R(x)= − 2345 + 104 = 7655 and R(y) = y = 3674. First represent x and y with five digits: R(x) = 97655 and R(y) = 03674. Then compute y′ = 96325, cin = 1 − bin = 0 and (R(x) + y′ + cin) mod 105 = (97655 + 96325) mod 105 = 93980. The integer represented by 93980 is equal to 93980 − 105 = −6020, that is, −2345−3674− 1.
In some cases it may be necessary to know whether the result of an operation actually is an (n + 1)-digit number and not an n-digit one. A typical case is the arithmetic unit of a general-purpose computer: both the operands and the result are n-bit numbers, and an overflow flag is raised if the result does not fit within n bits. Assume that the previous algorithms (addition, inversion, and subtraction) are executed without extending the operands to n + 1 bits:
So, if x and y are nonnegative, the sum x + y + cin could be greater than or equal to Bn/2. As R(x) = x and R(y) = y, then R(z) = (x + y + cin) mod Bn, and according to the previous hypothesis and to (4.16)
that is,
so that z(n) = 0 and z(n − 1) ≥ B/2.
The conclusion is that the sum of two nonnegative numbers, plus an initial carry, generates an apparently negative number if only n digits are available (z(n − 1) ≥ B/2).
If x and y are negative the sum x + y + cin could be smaller than − Bn/2. As R(x) = Bn + x and R(y) = Bn + y, then R(z) = (2.Bn + x + y + cin) mod Bn, and according to the previous hypothesis and to (4.16)
that is,
so that
The conclusion is that the sum of two negative numbers, plus an initial carry, generates an apparently nonnegative number if only n digits are available (z(n − 1) < B/2).
To summarize, the overflow detection is carried out just looking at the sign digits of the operands and the result. Under Boolean form:
If x ≥ 0 and y < 0 the difference x − y − bin could be greater than or equal to Bn/2. As R(x) = x, R(y) = Bn + y, y′ + 1 = Bn − R(y) = −y, R(z) = (R(x) + y′ + 1 − bin) mod Bn = (x − y − bin) mod Bn, then according to the previous hypothesis and to (4.19)
so that z(n) = 0, z(n − 1) ≥ B/2.
The conclusion is that the difference between a nonnegative number and a negative one, minus an initial borrow, generates an apparently negative number if only n digits are used (z(n − 1) ≥ B/2).
If x < 0 and y ≥ 0 the difference x − y − bin could be smaller than − Bn/2. As R(x) = Bn + x, R(y) = y, y′ + 1 = Bn − R(y) = Bn − y, R(z) = (R(x) + y′ + 1 − bin) mod Bn = (2.Bn + x − y − bin) mod Bn, then according to the previous hypothesis and to (4.19)
so that z(n) = 1, z(n − 1) < B/2.
The conclusion is that the difference between a negative number and a nonnegative one, minus an initial borrow, generates an apparently nonnegative number if only n digits are used (z(n − 1) ≥ B/2).
As in the preceding cases the overflow detection is carried out just looking at the sign digits of the operands and the result. Under Boolean form:
Examples 4.4 (B = 10, n = 4)
Comments 4.3 About the reduced B's complement representation (Comment 3.2):
If, furthermore, B > 2 and B is even, so that B ≥ 4, then 2.Bn−1 ≤ Bn/2 and
Thus both x + y + cin and x − y − bin are n-digit B's complement numbers. There is an overflow if the result is not a reduced B's complement number, that is, if the sign digit does not belong to {0, B − 1}. Actually the sign digit is equal to B−2 in the case of a negative overflow (result < − Bn−1) and to 1 in the case of a positive one (result ≥ Bn−1).
Examples 4.5 (B = 10, n = 3, 10's complement reduced form)
The addition and subtraction algorithms are based on the following properties.
Properties 4.4 Given two excess-E integers x and y, an initial binary carry cin and an initial borrow bin, then
Proof According to Definition 3.3, R(x) = x + E, R(y) = y + E, and R(x + y + cin) = x + y + cin + E, so
If x and y are two n-digit excess-E integers, and if z = x + y + cin is also an n-digit excess-E integer, then a straightforward addition algorithm consists in representing x and y with n + 1 digits, adding them up with cin and subtracting E. The result R(z) is an (n + 1)-digit natural number whose first digit is 0.
Assume that a procedure natural_subtraction has been defined:
procedure natural_subtraction (s: in natural; borrow: in bit; x, y: in digit_vector(0..s-1); next_borrow: out bit; z: out digit_vector(0..s-1);
The following algorithms compute z = x + y + cin.
Algorithm 4.22 Excess-E Addition
x(n):=0; y(n):=0; natural_addition(n+1, x, y, c_in, w, not_used); natural_subtraction(n+1, w, E, 0, z, not_used); if z(n)>0 then overflow:=true; end if;
Similar algorithms can be defined for computing z = x − y − bin and z = − x:
Algorithm 4.23 Excess-E Subtraction
x(n):=0; y(n):=0; natural_addition(n+1, x, E, 0, w, not_used); natural_subtraction(n+1, w, y, b_in, z, not_used); if z(n)>0 then overflow:=true; end if;
Algorithm 4.24 Excess-E Sign Change
x(n):=0; E_by_2(0):=0; for i in 1..n loop E_by_2(i):=E(i-1); end loop; natural_subtraction(n+1, E_by_2, x, 0, z, not_used); if z(n)>0 then overflow:=true; end if;
Examples 4.6 (B = 10, n = 4, excess 5000)
Given two n-digit sign-magnitude integers x and y, then z = x + y + cin is an (n + 1)-digit sign-magnitude integer. The following algorithm computes z.
Algorithm 4.25
if sign(x)=sign(y) then a:=abs(x)+abs(y); else a:=abs(x)- abs(y); end if; if a<0 then sign(z):=sign(y); abs(z):=-a; else sign(z):= sign(x); abs(z):=a; end if;
It is equivalent to the following algorithm (at the digit level).
Algorithm 4.26 Sign-Magnitude Addition
abs_x:=x(0..n-2)&0; abs_y:=y(0..n-2)&0; if x(n-1)=y(n-1) then natural_addition(n, abs_x, abs_y, 0, a, not_used); else for i in 0..n-1 loop abs_y′(i):=B-1-abs_y(i); end loop; natural_addition(n, abs_x, abs_y′, 1, a, not_used); end if; if a(n-1)=B-1 then z(n):=y(n-1); for i in 0..n-1 loop a′(i):=B-1-a(i); end loop; natural_addition(n, 0, a′, 1, z(0..n-1), not_used); else z(n):=x(n-1); z(0..n-1):=a; end if;
Example 4.7 (B = 10, n = 5). Assume that x = +2345 and y = − 7674. First express the absolute values with five digits: abs(x) = 02345 and abs(y) = 07674. As the signs are different, compute 02345 + 92325 + 1 = 94671. The first digit is equal to 9, indicating a negative value. The sign of the result is the same as the sign of y (−) and the absolute value of the result is 05328 + 1 = 05329. So the final result is −5329.
Comment 4.4 With algorithm 4.26, if sign(x) = 0, sign(y) = 1, and abs(x) = abs(y), the result is +0; if sign(x) = 1, sign(y) = 0, and abs(x) = abs(y), the result is − 0.
The subtraction x − y is equivalent to the addition x + minus_y where minus_y = − y, and the computation of minus_y is straightforward:
minus_y(n-1):=1-y(n-1); minus_y (0..n-2):=y(0..n-2);