COMP 421 - Lec5 - Computer Arithmetic 20142015 Students
COMP 421 - Lec5 - Computer Arithmetic 20142015 Students
ARITHMETIC
Electronics Engineering Department
Faculty of Engineering
University of Santo Tomas
COMP421 UST-ECE
©engradc 2010
©engrkad 2013
Different Data Types
Unsigned Notations
Unsigned Non-negative
Unsigned 2’s complement
27 26 25 24 23 22 21 20
-27 26 25 24 23 22 21 20
Two’s
BINARY Non-Negative
Complement
0000 0000 0 0
0111 1111 127 127
1000 0000 128 -128
1000 0001 129 -127
1111 1111 255 -1
Arithmetic Operation
Examples:
a. (+7) + (+4)
b. (+15) + (-6)
c. (+16) + (-24)
d. (-5) + (-9)
e. (+9) – (+3)
1 1 1111 111
7 d 0000 0111 b 255 d 1111 1111b
+ 4 d 0000 0100 b + 1d 0000 0001 b
11 d 0000 1011 b 256 d 0000 0000 b
=0
out of range
1 1 111 11
127 d 0111 1111 b 127 d 0111 1111 b
+ 128 d 1000 0000 b + 150 d 1001 0110 b
255 d 1111 1111 b 277 d 0001 0101 b
out of range = 21
XX–Y
X X + ( –Y)
1 1111 11
2d 0000 0010 b 0000 0010 b
– 1d – 0000 0001 b + 1111 1111 b
1d 0000 0001 b
1 1111 11
15 d 0000 1111 b 0000 1111 b
– 6d – 0000 0110 b + 1111 1010 b
9d 0000 1001 b
Non-Negative Subtraction
XX–Y
X X + ( –Y)
1d 0000 0001 b 0000 0001 b
– 2d – 0000 0010 b + 1111 1110 b
-1 d out of range
1111 1111 b
= 255
wrong answer
1 0 1111 111
7d 0000 0111 b 127 d 0111 1111b
+ 4d 0000 0100 b + 1d 0000 0001 b
11 d 0000 1011 b 128 d 1000 0000 b
out of range = -128
wrong answer
1 1111 111 1
-127 d 1000 0001 b -128 d 1000 0000 b
+ -1 d 1111 1111 b + -1 d 1111 1111 b
-128 d 1000 0000 b -129 d 0111 1111 b
out of range = 127
wrong answer
OVERFLOW: Carry Out ≠ Carry In (to MSB)
Two’s Complement Subtraction
XX–Y
X X + ( –Y)
1 1111 11
2d 0000 0010 b
– 1d + 1111 1111 b
1d 0000 0001 b
1 1111 11
15 d 0000 1111 b
– 6d + 1111 1010 b
9d 0000 1001 b
Two’s Complement Subtraction
XX–Y
X X + ( –Y) OVERFLOW:
0 0000 000 Carry Out ≠
1d 0000 0001 b Carry In (to MSB)
– 2d + 1111 1110 b
-1 d 1111 1111 b
Valid answer = -1
1 0000 000
-128 d 1000 0000 b
– 1d + 1111 1111 b
-129 d 0111 1111 b
= 127 Wrong answer
out of range
Binary Adder/Subtractor Implementation
X Y (1’s comp Y)
0 1
MUX Select ?
Addition Process
ADD: XX+Y
Arithmetic overflow
The result of addition process is out of range
Setting an overflow flag
Therefore:
Overflow occurs when the carry out is 0
Overflow in 2’s Complement Integer
Decimal
Unpacked BCD Packed BCD
Number
Examples:
a) 35 + 23 c) 35 + 66
b) 7 + 5 d) 8 + 9
BCD Addition
01 111
35 d 0011 b 0101 b
+ 23 d 0010 b 0011 b
58 d 0101 b 1000 b
5 8
BCD Addition
1 111
7d 0000 b 0111 b
+ 5d + 0000 b + 0101 b
11
12 d 0001 b 1100 b INVALID BCD
+ 0110 b Correction Factor
1 1 11 1
35 d 0011 b 0101 b
+ 66 d + 0110 b + 0110 b
111 111
101 d 1010 b 1011 b
+ 0110 b + 0110 b
0000 b 0001 b
BCD Addition
1
8d 1000 b
+ 9d + 1001 b WRONG RESULT!
17 d 0001 b VALID BCD
+ 0110 b Correction Factor
0111 b
BCD Addition
2 possible errors:
The sum is an invalid BCD code (greater than 9)
The sum is a valid BCD code (0-9) but not the correct
result
Example:
7 +5
35 + 66
8 + 9
Solution:
Add a correction factor (0110)
BCD Adder Implementation
X Y
4 4
Cout1 Cin
Parallel Adder 0110 0000
4 4
4 1 0
MUX Select ?
Cout
4
Cout2
Parallel Adder Cin = 0
4
X+Y
BCD Subtraction
Examples:
a) 9 – 6 c) 17 – 9
b) 25 – 11 d) 41 – 18
BCD Subtraction
0 1
9d 1001 b
– 6d – 0110 b
3d 0 011 b
3
0
25 d 0010 b 0101 b
– 11 d – 0001 b – 0001 b
14 d 0 0 01 b 010 0 b
1 4
BCD Subtraction
0
17 d 0001 0111 b
– 9d – 0000 1001 b
8d 0000 111 0 b INVALID BCD
0 – 0110 b Correction Factor
1 0 00 b
8
BCD Subtraction
01 1
41 d 0100 0001 b
– 18 d – 0001 1000 b
01
23 d
0010 1 0 01 b VALID but WRONG
2 – 0110 b Correction Factor
0 011 b
3
BCD Subtraction
If the initial difference contains BCD digit greater
than 9 (or 1001) or if a borrow from the next
higher digit was required to do subtraction then,
a correction factor of 6 (or 0110) must be
subtracted from the initial difference.
0110
X–Y
BCD Subtractor Implementation
X 9’s comp Y
Cin = 1
0110 0000
Parallel Adder
Cout1
1 0
MUX Select ?
X–Y
Floating Point Binary Number
Represents numbers in scientific notation and is capable of
representing large and small numbers without an increase
in the number of bits.
Composed of sign, significand (or mantissa) and exponent
[sign]1.[significand]2 x 2 [exponent]
Types
Single Precision Floating Point
Double Precision Floating Point
Single Precision Floating Point
Binary Number
Uses 32 bits
Has a sign bit, 8-bit biased exponent and a 24-bit
mantissa (but only the 23-bit fractional part of it is
presented, it contains 1 implied hidden bit)
Sign bit = 1 if negative, 0 if positive
8-bit biased exponent = 127 + exponent of the
normalized real number
Single Precision Floating Point
Binary Number
Single Precision Floating Point Format
B B B B B
31 30 23 22 0
Sign
Bit Biased Exponent Significand or Mantissa
Double Precision Floating Point Binary
Number
Uses 64 bits
Has a sign bit, 11-bit biased exponent and a 53-bit
mantissa (but only the 52-bit fractional part of it is
presented, it contains 1 implied hidden bit)
Sign bit = 1 if negative, 0 if positive
11-bit biased exponent = 1023 + exponent of the
normalized real number
Double Precision Floating Point
Binary Number
Double Precision Floating Point Format
B B B B B
63 62 52 51 0
Sign
Bit Biased Exponent Significand or Mantissa
Single and Double Precision:
COMPARISON
Single precision
SIGN EXPONENT (8 bits) MANTISSA (23 bits)
31 30 23 22 0
Double precision
EXPONENT (11 bits)
63 62 52 51 0
Floating Point Binary Number
Steps in converting decimal number to its floating
point equivalent
Convert the decimal number to binary
Express the binary number into its normalized form
(in scientific notation)
Determine the binary value of the sign bit and biased
exponent
Arrange the sign bit, biased exponent and fractional
part of the mantissa.
Example
1. Represent the following decimal numbers in single
precision floating point format
a. +12.25
b. – 0.75
c. –45.5625
2. Represent the following decimal number in double
precision floating point format
a. +519
b. –314.1592
American Standard Codes for Information
Interchange (ASCII)
ASCII
0 – 9 011 XXXX
A – Z 100 XXXX to 101 XXXX
a – z 110 XXXX to 111 XXXX