COMPUTER
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
Unpacked and Packed Binary Coded Decimal
(BCD)
Single and Double Precision Floating Point
Format
Alphanumeric Codes
Unsigned Non-Negative
Binary numbers are always treated as positive
numbers
For 8-bit binary number
27 26 25 24 23 22 21 20
For 16-bit binary number
215 214 213 212 211 210 29 28 27 26 25 24 23 22 21 20
Unsigned Two’s Complement
MSB of the signed binary number is the sign bit
indicates whether the number is positive or negative
For 8-bit binary number
-27 26 25 24 23 22 21 20
For 16-bit binary number
-215 214 213 212 211 210 29 28 27 26 25 24 23 22 21 20
Unsigned Notation
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)
Unsigned integers cannot represent negative numbers. A negative sign on the
number implicitly defines a subtraction operation.
To perform subtraction, perform 2’s complement addition on the
subtrahend.
Non-Negative Addition
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
OVERFLOW: Carry Out = 1
Non-Negative Subtraction
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
6d 0000 0110 b 0000 0110 b
– 15 d – 0000 1111 b + 1111 0001 b
-9d 1111 0111 b
out of range
= 247
wrong answer
OVERFLOW: Carry Out = 0
Two’s Complement Addition
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 ?
Cout Parallel Adder Cin
Overflow in Non-Negative Integer
Addition Process
ADD: XX+Y
Arithmetic overflow
The result of addition process is out of range
Setting an overflow flag
Theresult must be in the range: 0 to 2n – 1
Example:
1111 1111b + 0000 0001b = 1 0000 0000b
Therefore: Overflow occurs when the carry out is 1
Overflow in Non-Negative Integer
Subtraction Process
SUB: X X – Y or X X + Y’ + 1
Overflow occurs when the result is less than zero
Examples:
2–1=1
255 – 254 = 1
1 – 2 = –1
254 – 255 = –1
Therefore:
Overflow occurs when the carry out is 0
Overflow in 2’s Complement Integer
Addition / Subtraction Process
ADD: XX+Y SUB: X X + Y’ + 1
The result must be in the range: –2n –1 to 2n –1 – 1
Overflow only occurs when two numbers with the same
sign are added.
Examples:
126 + 1 = 127
–127 + (–1) = –128
127 + 1 = 128
–128 + (–1) = –129
Therefore: Overflow occurs when the carry out
and the carry in to MSBs are not equal
Unpacked and Packed BCD Format
BCD is used in computers to store decimal data
Decimal
Unpacked BCD Packed BCD
Number
18 00000001 00001000 0001 1000
905 00001001 00000000 00000101 1001 0000 0101
BCD Addition
BCD sum cannot have digit with a value greater
than 9.
Therefore, a carry must be generated if the
result of addition is greater than 9 (or 1001).
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
VALID BCD 0010 b
BCD Addition
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.
Now, we already know how to perform BCD
subtraction. But, how do we implement it in our
design?
BCD Subtractor Implementation
X 9’s comp Y
Parallel Adder Cin = 1
Cout1
0110
Cout2 Parallel Adder Cin = 0
(discarded)
X–Y
BCD Subtractor Implementation
X 9’s comp Y
Cin = 1
0110 0000
Parallel Adder
Cout1
1 0
MUX Select ?
Cout2 Parallel Adder Cin = 0
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)
SIGN MANTISSA (52 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