Chapter II CAO
Chapter II CAO
College (ATTC)
Menschen fuer Menschen Foundation
CHAPTER TWO
COMPUTER ARITHMETIC
BY
Mr. Chala Tesfaye
❑ Based on the use of simple digital logic devices that can store binary digits and perform
simple Boolean logic operations.
2
INTEGERS REPRESENTATION
❑ Integers: numbers without decimal point/fraction parts.
❑ Binary numbers can represent integers and other values using only binary
digits (0 and 1).
❖ One is that addition and subtraction require a consideration of both the signs of the
numbers and their relative magnitudes to carry out the required operation.
❖ Another drawback is that there are two representations of 0.
4
TWOS COMPLEMENT REPRESENTATION
❑ 1’s complement = 0’s to 1 and 1’s to 0
❑ 2’s complement = 1’s complement +1
❑ Ex: 1’s complement of 10101 is 01010 and Ex: 2’s complement of 10101 is 01011
❑ Overflow occurs if the sum cannot be expressed with the same bit of operands including
the sign bit.
❑ Overflow Rule: If two numbers are added, and they are both positive or both negative,
then overflow occurs if and only if the result has the opposite sign.
6
❑ EX: -7+5, and -7+-6
EXAMPLES
1001 = –7 1100 = –4
1001 = –7 +0101 = 5 1100 =+0100
–4 = 4
+0101 = 5 1110 = –2 +0100 =10000
4 = 0
1110 = –2 10000 = 0
(a) (–7) + (+5) (b) (–4) + (+4)
(a) (–7) + (+5) (b) (–4) + (+4)
0011 = 3
0011 = 3 1100
1100 = –4
= –4
+0100 = 4 +0100 = 4 +1111 =+1111
–1 = –1
0111 = 7 0111 = 7 11011 =11011
–5 = –5
(c) (+3) + (+4) (c) (+3) + (+4) (d) (–4) + (d) (–4) + (–1)
(–1)
1011 = –5 0101 = 5
❑ The last two examples 1011 = +1110
–5 = –2 0101 = 5
+0010 = 2
+1110 = 11001
–2 = –7 +0010 = 0111
2 = 7
11001 = –7 0111 = 7
demonstrate that the overflow (c) M =–5 = 1011 (d) M = 5 = 0101
(c) M =–5 = S1011
= 2 = 0010 (d) M = 5 =S 0101
=–2 = 1110
S = 2 =–S0010 S =–2 –S
= 1110
rule still applies. –S =
=
1110
1110
–S =
=
0010
0010
0111 = 7 1010 = –6
0111 = +0111
7 = 7 1010 = –6 = –4
+1100
+0111 = 71110 = Overflow +1100 = –4 = Overflow
10110
1110 = Overflow 10110 = Overflow
(e) M = 7 = 0111 (f) M = –6 = 1010 9
(e) M = 7 =S 0111
= –7 = 1001 (f) M = –6 S= =1010
4 = 0100
S = –7 –S
= 1001
= 0111 S = 4–S= =0100 1100
–S = 0111 –S = 1100
MULTIPLICATION
❑ Unsigned integers multiplication Using paper and pencil
1. Multiplication involves the generation of partial products, one for each digit in the
multiplier. These partial products are then summed to produce the final product.
2. The partial products are easily defined. When the multiplier bit is 0, the partial
product is 0. When the multiplier is 1, the partial product is the multiplicand.
3. The total product is produced by summing the partial products. For this operation,
each successive partial product is shifted one position to the left relative to the
preceding partial product
11
Figure 10.7 Multiplication of Unsigned Binary Integers
n-Bit Adder
Add
MULTIPLICATION
Shift and Add
Control Logic
❑ Example:
C An-1 A0 Qn-1 Q0
❖ Multiplier(Q)=1101(13)
(a) Block Diagram
❖ Solution: ? 1000 1111 (143)
C A Q M
0 0000 1101 1011 Initial Values
13
SIGNED NUMBER MULTIPLICATION
❑ Booth’s Algorithm
14
SIGNED NUMBER MULTIPLICATION
❑ Booth’s Algorithm
❑ Straight-forward multiplication will not work if multiplicand is negative
❑ The problem is that each contribution of the negative multiplicand as a partial product must
be a negative number on a 2n-bit field; the sign bits of the partial products must line up.
❑ Note that this is accomplished by padding each partial product to the left with binary 1s.
❑ EX: if multiplicand is negative
15
SIGNED NUMBER MULTIPLICATION
❑ Booth’s Algorithm
17
BOOTH’S ALGORITHM - EXERCISES
❑ Use booth’s algorism to find product of:
A. 7*3
B. 7 * -3 0111 0111
´0011 (0) ´1101 (0)
C. -7 * 3 11111001 1–0 11111001 1–0
0000000 1–1 0000111 0–1
D. -7 * -3 000111 0–1 111001 1–0
00010101 (21) 11101011 (–21)
(a) (7) ´ (3) = (21) (b) (7) ´ (–3) = (–21)
1001 1001
´0011 (0) ´1101 (0)
00000111 1–0 00000111 1–0
0000000 1–1 1111001 0–1
111001 0–1 000111 1–0
11101011 (–21) 00010101 (21)
18
(c) (–7) ´ (3) = (–21) (d) (–7) ´ (–3) = (21)
DIVISION
Paper and Pencil approach (long division)
❑ First, the bits of the dividend are examined from left to right, until the set of bits
examined represents a number greater than or equal to the divisor; this is referred to as
the divisor being able to divide the number.
❖ Until this event occurs, 0s are placed in the quotient from left to right.
❖ When the event occurs, a 1 is placed in the quotient and the divisor is subtracted from
the partial dividend. The result is referred to as a partial remainder
Paper and Pencil based
❑ From this point on, the division follows a cyclic pattern.
❑ At each cycle, additional bits from the dividend are appended to the partial remainder
until the result is greater than or equal to the divisor.
❑ As before, the divisor is subtracted from this number to produce a new partial remainder.
19
❑ The process continues until all the bits of the dividend are exhausted.
DIVISION-MACHINE ALGORITHM CORRESPONDS TO LONG DIVISION
DIVISION – EXAMPLE(7/3)
20
SIGNED NUMBER DIVISION
❑ To deal with negative numbers, we recognize that the remainder is defined by:
❑ D=Q*V+R
❑ Hence, one way to do twos complement division is to convert the operands into unsigned values
21
and, at the end, to account for the signs by complementation where needed.
FLOATING-POINT ARITHMETIC
❑ Floating-point arithmetic is a method used in computer systems to represent and
perform operations on real (decimal) numbers, efficiently handling very large or very
small values.
❑ Floating-point representation balances precision and range, making it ideal for scientific
and mathematical computations involving fractions or extreme magnitudes.
❑ In contrast, fixed-point notation represents numbers in a binary system with a fixed
position for the binary (or radix) point, enabling the representation of both integers and
fractional values.
❑ This format allows for the representation of integers as well as numbers with fractional
components by assuming a fixed binary point's position.
22
❑ EX: 00011001.01001
FLOATING –POINT REPRESENTATION
Limitation of fixed-point representation
❑ Limited Range: Cannot represent very large numbers or tiny fractions because the
number of bits is fixed.
❑ Loss of Precision: When dividing large numbers, the fractional part of the result might be
lost if there are not enough bits to store it.
❖ Significant (S): Also called the "mantissa," this stores the main digits of the number.
❖ Exponent (E): Indicates the position of the radix (decimal) point, making the number
larger or smaller.
❑ Base (B): The base is implicitly fixed (e.g., base 2 for binary), so it does not need to be
stored.
❑ Radix Point: The radix (decimal) point is assumed to be just to the right of the leftmost
bit in the significant.
…FLOATING-POINT REPRESENTATION
❑ Typical 32-bit Floating-Point Format:
1. Sign bit (1 bit):
❖ The leftmost bit stores whether the number is positive or negative.
❖ Example: 0 = positive, 1 = negative.
2. Exponent field (8 bits): The next 8 bits store the exponent in biased representation.
❖ A bias (fixed value) is subtracted to get the actual exponent.
❖ Example: If bias = 127, an exponent field of 130 gives the true exponent:
❖ 130−127=3.
3. Significant/Mantissa: The rest of the bits store the significant digits of the number.
❑ Why use this format? It allows representation very large and very small numbers
efficiently.
FLOATING-POINT REPRESENTATION
❑ In a 32-bit floating-point system, the exponent uses 8 bits to represent its value.
3. To get the true exponent: Subtract the bias value (127) from the stored exponent.
❖ Given a number that is not normalized, the number may be normalized by shifting the
radix point to the right of the leftmost 1 bit and adjusting the exponent accordingly.
FLOATING-POINT REPRESENTATION
❑ Note the following from the example below:
❖ The sign is stored in the first bit of the word.
❖ The first bit of the true significant is always 1 and need not be stored in the significant
field.
❖ The value 127 is added to the true exponent to be stored in the exponent field.
❖ The
base is 2.
❑ Examples
❑ This standard was created to ensure the portability of numerical programs across
different computing systems and to facilitate the development of sophisticated,
numerically intensive software applications.
❑ Its widespread adoption ensures that floating-point operations are consistent across
processors, which is essential for ensuring the accuracy and reliability of scientific,
engineering, and mathematical computations.
IEEE STANDARD FOR BINARY FLOATING-POINT REPRESENTATION
Key Aspects of IEEE 754:
32-bit Single Precision Format (Single Format):
❑ The single precision format uses 32 bits to represent a floating-point number.
❑ It divides these 32 bits into three parts:
❖ 1 bit for the sign (S)
❖ 8 bits for the exponent (E)
❖ 23 bits for the mantissa (M)
❑ The exponent is stored with a bias of 127, meaning the actual exponent is calculated by
subtracting 127 from the stored value.
❑ The mantissa is normalized, which means that the most significant digit is always
assumed to be 1, though it is not explicitly stored.
IEEE STANDARD FOR BINARY FLOATING-POINT REPRESENTATION
Key Aspects of IEEE 754:
64-bit Double Precision Format (Double Format):
• The double precision format uses 64 bits to represent a floating-point number.
• The 64 bits are divided into:
❖ The exponent is stored with a bias of 1023, and like single precision, the mantissa is
normalized (with an implicit leading 1).
FLOATING-POINT ARITHMETIC
❑Addition and Subtraction
❖To add or subtract two floating-point numbers, both numbers must have the same exponent.
❖Aligning the Radix Point: The radix point (decimal or binary point) of one number might
need to be shifted to align with the other number, making the exponents the same.
❖Multiplying and dividing floating-point numbers is simpler because you can directly add
the exponents for multiplication or subtract the exponents for division without needing to
align the radix points.
FLOATING-POINT ARITHMETIC
Possible Issues in Floating-Point Operations
1. Exponent Overflow: This happens when the exponent is too large to be represented.
o Result: The number may be shown as +∞ or -∞ (infinity).
2. Exponent Underflow: Occurs when the exponent is too small to be represented.
o Result: The number is too small and is treated as 0.
3. Significant Underflow: This happens when too many digits of the mantissa (significant
part) are lost during alignment.
o Solution: Rounding is used to keep the value as accurate as possible.
4. Significant Overflow: Occurs when adding two numbers of the same sign causes the
mantissa to overflow (become too large).
o Solution: This can be corrected by realigning the mantissa or adjusting the
exponent to handle the overflow.
ADDITION AND SUBTRACTION
❑ There are four basic phases of the algorithm for addition and subtraction:
1. Check for zeros. 2. Align the significance.
3. Add or subtract the significance. 4. Normalize the result.
❑ Zero Check:
❖ Addition and subtraction are essentially the same operation, with the only difference being the sign.
• Therefore, if the operation is subtraction, the sign of the second number (the subtrahend) is changed to
its opposite.
❖ This is done by shifting the significance (mantissa) of the smaller number to the right, which may cause
some digits to be lost.
• These lost digits are less important (as the number is smaller).
❖ Continue shifting until the exponents are equal.
ADDITION AND SUBTRACTION
Addition
❑ Next, the two significant are added together, taking into account their signs.
❖ Because the signs may differ, the result may be 0
❑ There is also the possibility of a significant overflow by 1 digit.
❖ If so, the significance of the result is shifted right and the exponent is incremented.
❑ An exponent overflow could occur as a result; this would be reported and the operation
stopped.
Normalization:
❑ The final phase normalizes the result.
❑ Normalization consists of shifting significant digits left until the most significant digit is
nonzero.
ADDITION AND SUBTRACTION
❑ Examples: Represent 0.5 and 0.25 using the following floating-point representation:
➢ 6-bit normalized signed 2’s complement fraction mantissa (significant), including the
sign.
Range of representation is -4 to 3.
❑ Follow the steps of floating-point addition to find the sum of the two numbers.
EXAMPLES (CONTD.)
➢ 0.5 0.1 X 20
➢ 0.25 0.01 X 20
0 01000 100
40
EXAMPLES (CONTD.)
❑ Add 0.5 to 0.5
0.5 0.1 X 20 0 10000 100
0.5 0.1 X 20 0 10000 100
0 10000
3. Add Mantissas. + 0 10000
Overflow
1 00000 0 10000 101
Correction
1. Normalize the result. 41
• Already Normalized.
EXAMPLES (CONTD.)
Add 4 to 4
4 0.1 X 23 0 10000 111
4 0.1 X 23 0 10000 111
1. Check for zeros.
• None.
2. Align Mantissas(significant).
• Already Aligned.
❑ All of the operations on floating-point numbers have been simplified so that the same
hardware can be used for floating-point numbers and integers. 43
THANK YOU