Lec2 BinaryArithmetic
Lec2 BinaryArithmetic
Bin n
Ari ar a
le a
thm y o
eti Bo ebr
c Alg
DIGITAL
CIRCUITS
g Se
i n mi
i t ch y Te con
Sw heor ch du
no cto
T l og r
y
3+5 =8
0011 + = 1000
0101
0000 0000
1111 0001 1111 0001
16/0 15 0
Modulo-16 15
-0
(4-bit) 1100 12 4 0100 1100 12 -3 4 0100
universe
-7 7
8 7 8
0111
1000 1000
6 –6 6
–2 7 7 7
–0 –5 –7 1010 –8
1010 0111 1010 0111 0111
1000 1000 1000
(Preferred)
subtraction
000 addition
-1 111 001 +1
Pos
Neg
itiv
ativ
e
010 +2
e
-2 110
num
num
ber
ber
s
s
-3 101 011 +3
Example -128 64 32 16 8 4 2 1
1 1 1 1 1 1 0 1
– 128 + 64 + 32 + 16 + 8 + 4 + 1 = – 128 + 125 = – 3
Increase
0011 of 4-bit
1100
0100 binary
1011 integers
0101
1010 0110
0111
1001 1000
Forbidden fence
Fall 2015, Aug 19 . . . ELEC2200-002 Lecture 2 30
Adding Two Bits
s=a+b
a b
Decimal Binary
0 0 0 00
0 1 1 01
1 0 1 01
1 1 2 10
CARRY
SUM
carry sum
OR carry
AND
AND
HA
HA
a XOR sum
XOR
b
c
FA
a2
b2 FA2 s2
a1
b1 FA1 c2 s1
a0
b0 FA0 c1 s0
c0 = 0
Multiplexer
carry
0 adder s16-s31
a16-a31 16-bit
ripple 1
b16-b31
carry This is a carry-select adder
1 adder
Fall 2015, Aug 19 . . . ELEC2200-002 Lecture 2 37
Fast Adders
In general, any output of a 32-bit adder can
be evaluated as a logic expression in terms
of all 65 inputs.
Number of levels of logic can be reduced to
log2N for N-bit adder. Ripple-carry has N
levels.
More gates are needed, about log2N times
that of ripple-carry design.
Fastest design is known as carry lookahead
adder.
Fall 2015, Aug 19 . . . ELEC2200-002 Lecture 2 38
N-bit Adder Design Options
Type of adder Time complexity Space complexity
(delay) (size)
Ripple-carry O(N) O(N)
1000
0000 partial products
0000
1000 Basic algorithm: For n = 1, 32,
____________ only If nth bit of multiplier is 1,
1 0 0 1 0 0 0two = 72ten then add multiplicand × 2 n –1
to product
n = 32 n < 32
Done n=? n=n+1
64
after add
LSB
64 LSB = 0
after add
=1
64
64-bit product register, initially 0 3 operations per bit:
write shift right
shift left
add
Need 64-bit ALU
Fall 2015, Aug 19 . . . ELEC2200-002 Lecture 2 43
Serial Multiplication (Improved)
Multiplicand 2 operations per bit:
N = 32
shift right
32 add
32 32-bit ALU
(1) add 1
Test LSB
32-bit ALU
32 times
LSB
32
1 ir te
) w
(2 1 or 0
64-bit product register
(3) shift right
Full
adder Slide 24
p0i = x0yi carry bits
to (k+1)th sum bit
0 partial product
th
sum to (k+1)th
sum
Fall 2015, Aug 19 . . . ELEC2200-002 Lecture 2 52
y3 y2 y1 y0
Array Multiplier x0
ppk
yj
xi 0
x1
ci 0 0 0 0
FA 0
x2
co
ppk+1
0
x3
Critical path
0
0
FA FA FA FA
p7 p6 p5 p4 p3 p2 p1 p0
Fall 2015, Aug 19 . . . ELEC2200-002 Lecture 2 53
Types of Array Multipliers
Baugh-Wooley Algorithm: Signed product
by two’s complement addition or
subtraction according to the MSB’s.
Booth multiplier algorithm
Tree multipliers
Reference: N. H. E. Weste and D. Harris,
CMOS VLSI Design, A Circuits and
Systems Perspective, Third Edition,
Boston: Addison-Wesley, 2005.
Fall 2015, Aug 19 . . . ELEC2200-002 Lecture 2 54
Binary Division (Unsigned)
13 Quotient 00001101
11/147 Divisor / Dividend 1 0 1 1 / 1 0 0 1 0 0 1 1
11 1011
37 Partial remainder 001110
33 1011
4 Remainder 001111
1011
100
Iteration 2 Iteration 1
Divisor: 4 = 0100 1100
– 4 = 1100 1100 negative → quotient bit 0
0100 → restore remainder
6
─ = 1, remainder 2 0000110
4 1100
1101 negative → quotient bit 0
0100 → restore remainder
000110
Iteration 4 Iteration 3
1100
1111 negative → quotient bit 0
0100 → restore remainder
00110
1100
0010 positive → quotient bit 1
$R (33 b) | $Q (32 b)
Shift 1-bit left $R, $Q
$R and $M have
one extra sign bit
$R ← $R – $M beyond 32 bits.
No Yes $Q0=0
$Q0 = 1 $R < 0?
$R ← $R + $M
Restore $R
count = count – 1 (remainder)
No Yes Done
count = 0? $Q = Quotient
$R = Remainder
Fall 2015, Aug 19 . . . ELEC2200-002 Lecture 2 57
count 4-bit Example: 6/4 = 1, Remainder 2
Actions n $R, $Q $M = Divisor
4 Initialize 4 00000|0110 00100
Shift left $R, $Q 4 00000|1100 00100
Add – $M (11100) to $R 4 11100|1100 00100
Restore, add $M (00100) to $R 3 00000|1100 00100
3 Shift left $R, $Q 3 00001|1000 00100
Add – $M (11100) to $R 3 11101|1000 00100
Restore, add $M (00100) to $R 2 00001|1000 00100
2 Shift left $R, $Q 2 00011|0000 00100
Add – $M (11100) to $R 2 11111|0000 00100
Restore, add $M (00100) to $R 1 00011|0000 00100
1 Shift left $R, $Q 1 00110|0000 00100
Add – $M (11100) to $R 1 00010|0000 00100
Set LSB of $Q = 1 0 00010|0001 00100
0 Remainder | Quotient
Fall 2015, Aug 19 . . . ELEC2200-002 Lecture 2 58
Division
33-bit $M (Divisor)
33 Step 2: Subtract $R ← $R – $M
33
33-bit ALU
Step 2, Add – $M = 1 1 1 0 1
$R = 1 1 1 1 1
Step 3, Set Q0 $Q = 0000
Restore + $M = 0 0 0 1 1
$R = 0 0 0 1 0
$R = 1 1 1 1 1
Step 3, Set Q0 $Q = 0 0 1 0 Final quotient
Restore + $M = 0 0 0 1 1
$R Remainder
= 00010
Note “Restore $R” in Steps 1, 2 and 4. This method is known as
the RESTORING DIVISION. An improved method, NON-
RESTORING
DIVISION, is possible (see Hamacher, et al.)
Fall 2015, Aug 19 . . . ELEC2200-002 Lecture 2 61
Non-Restoring Division
Avoid unnecessary addition (restoration).
How it works?
– Initially $R contains dividend ✕ 2 – n for n-bit numbers. Example (n = 8):
Non-restoring division
Initialize and start iterations same as in restoring
division by subtracting divisor
In any iteration after left shift and subtraction/addition
– If $R is positive, subtract divisor (y) in next iteration
– If $R is negative, add divisor (y) in next iteration
After final iteration, if $R is negative then restore it by
adding divisor (y)
Step 3, Set Q0
$R = 1 1 1 1 1 $Q = 0 0 1 0 Final quotient = 2
Step 3, Set Q0
Restore + $M = 0 0 0 1 1
$R = 00010 Remainder = 2
1915 -1998
Fall 2015, Aug 19 . . . ELEC2200-002 Lecture 2 70
The Idea of Hamming Code
N=4
Code space contains 2N possible N-bit code words: Code Symbol
HD = 4 HD = 3
HD = 3
0010010 HD = 1 HD = 3
1-bit error in “A” ”?” 1010010 0011110
”A” ”3”
shortest distance
decoding eliminates HD = 2
error
HD = 4
HD = 3 HD = 3
1000111
”8”
1011001
”B”
Fall 2015, Aug 19 . . . ELEC2200-002 Lecture 2 72
Minimum Distance-3 Hamming Code
Original Odd-parity
Symbol
code code
ECC, HD ≥ 3
Original code: Symbol “0” with a
0 0000 10000 0000000 single-bit error will be Interpreted as
1 0001 00001 0001011 “1”, “2”, “4” or “8”.
2 0010 00010 0010101
3 0011 10011 0011110 Reason: Hamming distance between
4 0100 00100 0100110 codes is 1. A code with any bit error will
5 0101 10101 0101101 map onto another valid code.
6 0110 10110 0110011
7 0111 00111 0111000
Remedy 1: Design codes with HD ≥ 2.
8 1000 01000 1000111
Example: Parity code. Single bit error
9 1001 11001 1001100
detected but not correctable.
A 1010 11010 1010010
Remedy 2: Design codes with HD ≥ 3.
B 1011 01011 1011001
For single bit error correction, decode
C 1100 11100 1100001
as the valid code at HD = 1.
D 1101 01101 1101010
E 1110 01110 1110100
For more error bit detection or
F 1111 11111 1111111
correction, design code with HD ≥ 4.
Fall 2015, Aug 19 . . . ELEC2200-002 Lecture 2 73
Integers and Real Numbers
Integers: the universe is infinite but discrete
– No fractions
– No numbers between consecutive integers, e.g., 5 and 6
– A countable (finite) number of items in a finite range
– Referred to as fixed-point numbers
Real numbers – the universe is infinite and continuous
– Fractions represented by decimal notation
Rational numbers, e.g., 5/2 = 2.5
Irrational numbers, e.g., 22/7 = 3.14159265 . . .
– Infinite numbers exist even in the smallest range
– Referred to as floating-point numbers
or (-1)S × (1+F) × 2E
Where
S = sign, 0 for positive, 1 for negative
F = fraction (or mantissa) as a binary integer,
1+F is called significand
E = exponent as a binary integer, positive or
negative (two’s complement)
= - (1 + 0.5 + 0.25)/4
= - 1.75/4
= - 0.4375 (decimal)
b. 1933, Canada
Professor of Computer Science, UC-Berkeley
Fall 2015, Aug 19 . . . ELEC2200-002 Lecture 2 79
Numbers in 32-bit Formats
Two’s complement integers
Expressible numbers
-231 0 231-1
1 10000001 01000000000000000000000
- (2 – 2 -23
)×2 127 -2-126 0 2-126 (2 – 2-23)×2127
+ 1.0 × 2 –127
Smaller than the smallest positive number in
single-precision IEEE 754 standard.
Interpreted as positive zero.
True exponent less than –126 is positive
underflow; can be regarded as zero.
– 1.0 × 2 –127
Greater than the largest negative number in
single-precision IEEE 754 standard.
Interpreted as negative zero.
True exponent less than –126 is negative
underflow; may be regarded as 0.
+ 1.0 × 2128
Greater than the largest positive number in
single-precision IEEE 754 standard.
Interpreted as + ∞
If true exponent > 127, then the number is
greater than ∞. It is called “not a number” or
NaN and may be interpreted as ∞.
–1.0 × 2128
Smaller than the smallest negative number in
single-precision IEEE 754 standard.
Interpreted as - ∞
If true exponent > 127, then the number is less
than - ∞. It is called “not a number” or NaN and
may be interpreted as - ∞.
–4
1.000two × 2 = (1+0)/16 = 0.0625ten
no no
X = 0? Y = 0? Steps 1 - 5
yes yes
Z=0
Return