Download Digital Logic: Encoding and Adding Unsigned Numbers in Binary and Other Bases and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!
Lecture 11
Lecture 11 – Chapter 5
Positional Number Representation
Addition of Unsigned Numbers
ECE 274 - Digital Logic
Encoding Numbers
Inputs to digital systems not limited to a single 1 or 0
Integer values, temperature, time,
currency, images
Previously, we said our digital system deal with 1s and 0s only
How do we encode these larger
values?
Look at encoding numbers
b
a F = a + b
average(5, 14)
ECE 274 - Digital Logic
Encoding Numbers: Decimal Numbers
Base ten (Decimal)
Each position represents a quantity
Symbol in position means how many
of that quantity
Ten symbols: 0, 1, 2, ..., 8, and 9 More than 9 -- next position So each position power of 10 Nothing special about base 10 -- used because we have 10 fingers
523 = (5 * 10^2 ) + (2 * 10^1 ) + (3 * 10^0 ) = (5 * 100) + (2 * 10) + (3 * 1)
Encoding Numbers: Binary Numbers (Base 2)
Q: How much?
Base two (Binary)
Two symbols: 0 and 1
More than 1 -- next position
So each position power of 2
1012 = (1 * 2^2 ) + (0 * 2^1 ) + (1 * 2^0 )
“one zero one”
“one hundred one”
ECE 274 - Digital Logic
Encoding Numbers: Binary Numbers
Range of integer binary number can represent depends on bits used
n bits can represent integers in the
range of 0 to (2 n^ – 1)
Most-significant bit (MSB)
Left-most bit
Associated with highest power of 2
Least-significant bit (LSB)
Right-most bit
Associated with lowest power of 2
Convenient to group bits together
4 bits – nibble
8 bits - byte
MSB LSB
nibble byte
ECE 274 - Digital Logic
Decimal Equivalent of a Binary Number – Example 1
What is the decimal equivalent?
110 2 =? Decimal
2 2 2 1 2 0
1 1 0
110 2 = (1 * 2 2 ) + (1 * 2^1 ) + (0 * 2^0 )
= (1 * 4) + (1 * 2) + (0 * 1)
= 6 (^10)
Converting from Decimal to Binary Numbers: Subtraction Method (Remainder)
Remaining quantity: 12
1 32 is too much
(^0 1) 16 is too much
(^0 0 1) 12 – 8 = 4
(^0 0 1 1) 4 - 4 = 0 DONE
0 answer
0 1 1 0 0
Subtraction method
To make the job easier (especially
for big numbers), we can just
subtract a selected binary weight
from the (remaining) quantity
Then, we have a new remaining quantity, and we start again (from the present binary position) Stop when remaining quantity is 0
ECE 274 - Digital Logic
Converting from Decimal to Binary Numbers: Division Method
Binary Number
quotient (6) is greater than 0, keep dividing by 2
Decimal Number
divide by 2
insert remainder
insert remainder
insert remainder
quotient (3) is greater than 0, keep dividing by 2
quotient (1) is greater than 0, keep dividing by 2
quotient (0) is 0, we can conclude that 12 is 1100 in binary
insert remainder
Division Method
Divide decimal number by
2 and insert remainder
into new binary number
Continue dividing quotient
by 2 until the quotient is
Example : Convert 12 into
binary
ECE 274 - Digital Logic
Converting from Decimal to Binary Numbers: Division Method Example 2
Binary Number
quotient (5) is greater than 0, keep dividing by 2
Decimal Number
divide by 2
insert remainder
insert remainder
insert remainder
quotient (2) is greater than 0, keep dividing by 2
quotient (1) is greater than 0, keep dividing by 2
quotient (0) is 0, we can conclude that 10 is 1010 in binary
insert remainder
Let’s try another example
using the Division Method
Convert 10 into binary
Encoding Numbers: Octal Representation (Base 8)
octal binary
Q: Write 11110000 2 in octal
Other bases available
Base 8 – Octal
Base 16 – Hexadecimal (hex)
Octal
Symbols
Range from 0 to 7 Useful shorthand for binary
Binary to Octal conversion
Group binary numbers in 3’s Replace with corresponding octal digit
Octal to Binary conversion
Replace octal digit with corresponding 3 bits denoting same value
Q: Write 754 8 in binary
Q: Write 27 8 in decimal
(2 * 8^1 ) + (7 * 8^0 ) = 23 10
Symbols = (0, 1, 2, 3, 4, 5, 6, 7)
ECE 274 - Digital Logic
Encoding Numbers: Hexadecimal Numbers (Base 16)
hex binary 1000 1001 1010 1011 1100 1101 1110 1111
A
B
C
D
E
F
hex bina ry
8 A F
Symbols = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F)
Q: Write 1111 0100 2 in hex
F 4 16
Q: Write 8AF 8 in binary
Q: Write AC 16 in decimal
(10 * 16^1 ) + (12 * 16^0 ) = 172 10
Hex
Each position represents four
base two positions
Used as compact means to write
binary numbers
Symbols range from 0 to F
Binary to Hex conversion
Group binary numbers in 4’s Replace with corresponding hex digit
Hex to Binary conversion
Replace hex digit with corresponding 4 bits denoting same value
ECE 274 - Digital Logic
Number Representation for Computers vs. for Humans
Modern computer represent numbers using 32 or 64 bits
Binary representation can be
confusing for humans
Datasheet, documentation, etc.
generally uses hex
Introduced simple numbers
Only positive values – unsigned
numbers
Later we’ll consider
Signed numbers (positive &
negative integers)
Fixed-point
Floating-point
C3 81 A00F 16
Exponential Growth of Two-Level Adders
What if we want to bigger numbers (i.e. multiple bit numbers)?
Addition of two 3-bit numbers
Addition of two 4-bit numbers
Addition of two n-bit numbers
C
B 2
S 2
B 1
S 1
B 0
S 0
A 2 A 1 A 0
truth table with 26 rows = 64 rows
C
B 3
S 3
B 2
S 2
B 1
S 1
B 0
S 0
A 3 A 2 A 1 A 0
truth table with 28 rows = 256 rows
10000 8000 6000 4000 2000 0 1 2 3 4 5 N
6 7 8
Transistors
Exponential Growth for Two-Level Adder Implementation
ECE 274 - Digital Logic
Addition of Unsigned Numbers with Carry
Instead we can consider each column separately
1 1 0 need to consider
possible carry bit from previous row
x
y
ci+1s
carry out sum
operand 2
operand 1 ci
carry in
based on this observation we can create a truth table
y sci+
cix
ECE 274 - Digital Logic
Full Adder
Let’s create circuit to perform addition
sum= x XOR y XOR ci
carry = xy + xc i + yc i
Special type of circuit – full adder
Adds three bits
y s ci+
cix
s
c (^) i+
x y
Circuit
ci
Graphical Symbol
x
FA
ci+1 s
y ci
Alternative Method to Design an Adder: Imitate Adding by Hand
A:
B:
A:
+ B: 0
Create component for each column
Adds that column’s bits, generates sum and carry bits
x FA c (^) i+1 s
y (^) c (^) i
1 SUM
x FA c (^) i+1 s
y (^) c (^) i
x FA c (^) i+1 s
y (^) c (^) i x FA c (^) i+1 s
y (^) c (^) i
ECE 274 - Digital Logic
Carry-Ripple Adder
A 3 A 2 A 1 A 0 B 3
ci+1s 3 s 2 s 1 s 0
c (^) i
B 2 B 1 B 0
Graphical Symbol
4-bit adder
Called a carry-ripple adder
4-bit adder shown: Adds two 4-bit numbers, generates 5-bit output
5-bit output can be considered 4-bit “sum” plus 1-bit “carry out”
Can easily build any size adder
A 3
FA
co
B 3 A 2 B 2
S 3 S 2 S 1
xyc (^) i
FA
xyc (^) i
A 1 B 1
FA
xyc (^) i
S 0
A 0 B 0 ci
FA
xyc (^) i
c (^) i+1 s c (^) i+1 s c (^) i+1 s c (^) i+1 s
ECE 274 - Digital Logic
Carry-Ripple Adder’s Behavior
0111 + 0001
(answer should be 01000)
Assume all inputs initially 0
Wrong answer -- something wrong? No -- just need more time for carry to ripple through the chain of full adders.
FA
xy ci FA
xyci
FA
xyci
FA
xyc (^) i
ci+1 s c (^) i+1 s ci+1 s ci+1 s 0 0 0
0 0 1 1 0 Output after 2ns (1 FA delay)
FA
xyci FA
xyci FA
xyci FA
xyci
c (^) i+1 s ci+1 s ci+1 s ci+1 s
Design Example: Multiple by 3
Note 3I = I + I + I
ci+
c i
s 7 s 6 s 5 s (^4)
B 7 B 6 B 5 B 4
8-bit adder
A 7 A 6 A 5 A 4 A 3 A 2 A 1 A 0 B 3 B 2 B 1 B 0
s 3 s 2 s 1 s (^0)
I 7 I 6 I 5 I 4 I 3 I 2 I 1 I 0 I 7 I 6 I 5 I 4 I 3 I 2 I 1 I 0
ci+
c (^) i
s 7 s 6 s 5 s (^4)
B 7 B 6 B 5 B 4
9-bit adder
A 7 A 6 A 5 A 4 A 3 A 2 A 1 A 0 B 3 B 2 B 1 B 0
s 3 s 2 s 1 s (^0)
A 8 B 8
s (^8)
I 7 I 6 I 5 I 4 I 3 I 2 I 1 I 0
P 9 P 8 P 7 P 6 P 5 P 4 P 3 P 2 P 1 P 0
ECE 274 - Digital Logic
Design Example: Multiply by 3 Using Shift Left
Alternatively we should shift bits in I one position to left
Shift Left Operation (<<) effectively multiplies value by 2
I << 2 = I * 2
ci+
c (^) i
s 7 s 6 s 5 s (^4)
B 7 B 6 B 5 B 4
9-bit adder
A 7 A 6 A 5 A 4 A 3 A 2 A 1 A 0 B 3 B 2 B 1 B 0
s 3 s 2 s 1 s (^0)
A 8 B 8
s (^8)
I 7 I 6 I 5 I 4 I 3 I 2 I 1 I 0
P 9 P 8 P 7 P 6 P 5 P 4 P 3 P 2 P 1 P 0
I 7 I 6 I 5 I 4 I 3 I 2 I 1 I 00
2I + I
3I
ECE 274 - Digital Logic
Shift Right, Same as Divide by 2
Shifting works other way too
Shift bits in I one position to right
Shift Right Operation (>>) effectively divides value by 2
I >> 2 = I / 2
Design Example: Compensating Scale
Design Example: Compensating Scale
Over time scale accuracy deteriorates
Design a circuit that will weigh an
object, and add an adjustment from a
user
Input
8-bit weight Reported by Sensor
3-bit User Adjustment
Output
8-bit sum of inputs
ECE 274 - Digital Logic
Design Example: Compensating Scale
ci+
c (^) i
to display
B 3 B 2 B 1 B 0
8-bit adder
Weight Sensor
Adjustment Knob
(^0 ) 6 2 543
7
Weight Adjuster
A 7 A 6 A 5 A 4 A 3 A 2 A 1 A 0 B 7 B 6 B 5 B 4
s 7 s 6 s 5 s 4 s 3 s 2 s 1 s (^0)
ECE 274 - Digital Logic
Design Example: 8-bit Calculator
Design Example: 8-bit Calculator
Add two numbers controlled by DIP
switches
Output result using eight LEDs
Input
8-bit input A
8-bit input B
Output
8-bit sum S