Chapter Binary arithmetic Arithmetic is the process of applying a mathematical operator (such as negation or addition) to one or more operands (the values being operated upon). Binary arithmetic works exactly like arithmetic, except that 2 (not ) governs how individual digits are interpreted and manipulated.. Addition To add a augend a and addend b, the sum s is constructed by adding digits one at a time from least to most significant digit. If in any position the result is 2,ais carried into the next more-significant position [Alg..]. 5 8 sum out in A non-zero out of the most significant digit indicates unsigned overflow; the result is larger than the largest value that can be represented (2 N, for N bits). 5 62 out in sum Two s complement signed addition is the same as unsigned addition (because 2 N the additive inverse of x, modulo 2 N [Sec. 2..4]). x is -5-2 sum in out to msb of msb in - -5-8 sum in out to msb of msb in If the signed result cannot be represented in the number of bits available then signed overflow will occur. Signed overflow is indicated by the in to the msb being different to the out of the msb. 9
Algorithm. Binary addition input: N-bit augend a and addend b, and a -bit in c output: N-bit sum s, and a -bit out c function ADD(a, b, c) for i to N do n a i b i c. two-bit sum s i n c n return c, s. out and sum 6 8 sum in out to msb of msb in signed overflow - -8-65 sum in out to msb of msb in signed overflow Signed overflow can occur only if the augend and addend have the same sign..2 Subtraction To subtract a subtrahend b from a minuend a, the difference d is constructed by subtracting digits one at a time from the least to the most significant digit. If in any position the resulting digit would be negative, a is ed from the next more-significant position. - - out - - - - difference A non-zero out of the most significant digit indicates unsigned overflow; the (negative) result is less than the smallest value that can be represented (zero). - - -66 (-) - difference out - - - A simpler, more convenient, and more efficient way to subtract b from a is to observe that, modulo 2 N, a b 2 N a b a (2 N b) a b
where b is the two s complement of b [Sec. 2..4]. Subtraction now behaves exacly like addition of signed numbers [Sec..], including the detection of signed overflow by comparing the carries in to and out of the msb. Propagating a is expensive [Sec. 5.4.]. Adding the two s complement of b to a involves propagating two separate carries (first when calculating the two s complement, and again when performing the addition with a). The first of these can be eliminated by observing that the in to the addition is always. Setting the in to instead, and then using the one s complement of b (which can be calculated very efficiently), produces exactly the same result. a b = a b (add the two s complement of b) = a b (add the one s complement of b plus ) Using this method of subtraction, the in to the lsb of the addition becomes a not in. (When c =, the subtraction yields the correct result; when c =, the subtraction yields one less than the correct result.) Similarly, the out from the msb of the addition represents the not out from the subtraction. For unsigned subtraction, an unsigned overflow has therefore occured if (and only if) the out is. (Most ALUs implement subtraction using this method. How programmers view and depends on the design of the CPU instruction set. Some CPUs, such as the ARM found in most mobile devices, use the directly and programmers must treat is as not. Others, such as Intel processors, invert the in and out when subtracting, and programmers treat as.). Multiplication To multiply a multiplicand a and multiplier b, the product p (initially ) is constructed by considering each bit position i in the multiplier. If b i =, the multiplicand is shifted left i bits (forming a partial product) and added to p [Alg..2]. NX p = b i a 2 i i= 6 5 product Signed multiplication is the same as unsigned multiplication, provided signed overflow is avoided during the addition of the partial products. The product p of two N-bit numbers p = a b can require up to 2N bits, so the mulitplication is converted to a 2N-bit multplication by sign-extending both multiplicand and multiplier to 2N bits. A 2N-bit unsigned multiplication of two signed numbers then gives the correct 2N-bit signed result.
Algorithm.2 Binary multiplication input: N-bit multiplicand a and multiplier b output: 2N-bit product p function MULTIPLY(a, b) p. register p is 2N bits wide for N times do if b =then p p a. accumulate partial products a a lshift b b rshift return p. product () (-5) (??) incorrect () (-5) sign extended (-5) (-) (-) sign extended (9) To obtain a N-bit result, take the least significant N bits of the 2N-bit result. If any of the most significant N bits are non-zero, then an unsigned overflow has occurred. If any of the most significant N bits differ from the sign bit p N of the result, then a signed overflow has occured..4 Division To divide a N-bit unsigned dividend a by an unsigned divisor b, set r to a and then the quotient q is the number of times b can be subtracted from r without the result becoming negative. When no more subtractions are possible, r contains the remainder and a = b q r, r<b. Because of the second condition (which implies b 6= ) there is only one solution to this equation. The manual process of long division is more efficient. quotient 7 2 2-2 - (cannot subtract) (-7=4) - (8-7=) 2 remainder
Algorithm. Unsigned division input: N-bit dividend a and divisor b output: N-bit quotient q and remainder r note: All registers are N bits wide, except for r : a which is the 2N-bit concatenation of r (most significant half) and a (least significant half). function UNSIGNEDDIVIDE(a, b) q r.r:ais the 2N-bit, zero-extended dividend for N times do q q lshift r:a r:a lshift. moves msb of dividend a to lsb of r if r b then r r b. subtract the divisor from the partial remainder q q. put the corresponding into the lsb of the quotient return q, r. unsigned quotient and remainder Manual long division can be simulated by initially shifting r (a copy of a) to the right N bits, and then building q one bit at a time, from msb to lsb. For each bit position, r is shifted left one bit and b subtracted from it. If the result is non-negative, a is appended to the least-significant end of q; otherwise a is appended to q, and r restored to its previous value. When all N bit positions have been considered, q contains a b and r contains the remainder. Signed, two s complement division can be performed by making dividend and divisor positive, performing an unsigned division, and then adjusting the signs of the quotient and remainder. As above, quotient and remainder are still defined by a = b q r, and r < b, but this is insufficient to avoid multiple solutions, because we do not know the correct signs of q and r. To make the solution unique, the usual convention is to require sign r =signa, and, if sign a =signb, sign q =, otherwise. Algorithms can be constructed to perform division directly on signed, two s complement dividend and divisor [Alg..4]. Regardless of the algorithm used, signed overflow can occur during division. For example, the two s complement signed result of the 8-bit division 28 cannot be represented in 8 bits.
Algorithm.4 Signed, two s complement, restoring division input: N-bit signed dividend n and divisor d output: N-bit quotient q and remainder r such that n = d q r, where r always has the same sign as n, q is negative if the signs of n and d differ, and apple r < d note: All registers are N bits wide, except for r : q which is the 2N-bit concatenation of r (most significant half) and q (least significant half). note: r : q initially holds a sign-extended dividend. Instead of shifting the divisor right at each division step, we shift the dividend r : q left (setting the lsb of q to ). The partial remainder is therefore always in r, and if we succeed in subtracting the divisor from it we can put a into the lsb of q. After N iterations, q contains the quotient and r contains the remainder. function SIGNEDDIVIDE(n, d) r:q sign-extend n. sign-extend dividend to 2N bits for N times do. form N quotient bits, one at a time, from msb to lsb r: q r: q lshift.r: q is the 2N-bit concatenation of N-bit registers r and q p r if sign r =signdthen.rand d are both positive or both negative r r d. move r towards else r r d. ditto if sign p =signq _ (r =^q =)then. subtraction did not cross q. shift a into the lsb of the quotient else. subtraction overflowed r p. restore r to previous value if r = d then. for negative n, the loop can produce r = d r. so adjust remainder and quotient if necessary q q. to guarantee r < d if sign n 6= signd then. the above computes a positive quotient q q. so make it negative if necessary return q, r. signed quotient and remainder 4