T1 Logic Design and Numbers
T1 Logic Design and Numbers
1
Why Study Digital Design?
• Many elements of our lives are or are to
become digital
– Computer, AI, IoT, camera, cell phone, TV,
car...
2
“Digital” vs. “Analog”
• Analog signal • Digital signal
– Infinite possible values – Finite possible values
• Ex: voltage on a wire • Ex: button pressed on a
created by microphone keypad
Sound waves
move the
membrane, 1 2 3 4
2
which moves digital
the magnet,
signal
microphone
analog
which creates
signal
current in the nearby wire
Possible values: Possible values:
4
value
1.00, 1.01, 2.0000009, 3
0, 1, 2, 3, or 4.
value
That’s it.
... infinite possibilities 2
1
0
time
time
3
Digital Signals with Only Two Values: Binary
• Binary digital signal -- only two possible
values
value
– Typically represented as 0 and 1, respectively
– Everything is represented as combinations of 1
0
0’s and 1’s, e.g. 1011, 11010 time
• Called binary value or binary number
• Each binary digit is a bit
– We’ll only consider binary digital signals
• Although there are other types of digital signals
– Binary digital signal is popular because
• Transistors, the basic digital electric
component, operate at two voltages: low (e.g.
0V or -5V) and high (e.g. 3.3V or 5V)
• Storing/transmitting one of two values is easier
than three or more
4
From Analog to Digital (A2D) – Digitization
• Analog signal (e.g., audio)
Volts
may lose quality 3 distortion 3
– Voltage levels not 2 2
saved/copied/transmitted 1 original signal 1 received signal
perfectly 0
0
– Hard to recover time time
• Digitized version: Hard to fix, higher? lower?
01 10 11 10 11
– “Sample” voltage at
particular rate A2D distortion
Volts
– Easy to distinguish 0s
from 1s, thus easy to digitized signal 1
1
recover
0 0
– Increase sample rate to
improve quality time time
Can fix -- easily distinguish 0s
Example:
01 10 11 10 11 and 1s, restore
if only 4 sampled values
let binary representation be: D2A
Volts
3
0 V: “00”
2
1 V: “01”
2 V: “10” 1
0 time 5
3 V: “11”
From Analog to Digital – Digitization
Volts
Volts
3 3
2 2
1 original signal 1 recovered signal
Restore 0
0
time
01 10 11 10 11 01 10 11 10 11
(5 values)
6
Resolution
What happened?
(source: cntv.cn)
(source: wikipedia.org)
7
From Analog to Digital – Digitization
Volts
Volts
3 3
2 2
1 original signal 1 recovered signal
Restore 0
0
time
01 10 11 10 11 01 10 11 10 11
(5 values)
Volts
3
2 Restore
1 original signal
0
time
sensors and
other inputs
electric digital
signal data
Analog to digital
converter A2D
digital
data
Digital System
digital digital
data data
Digital to analog D2A
converter
electric
signal
actuators and
other outputs
9
How to Represent Numbers with Bits?
• Number systems: decimal, binary, octal, hexadecimal, …
– Base ten (decimal)
5 2 3
4 3 2 1 0
10 10 10 10 10
1 0 0 1 1 . 0 1 0
×16 ×8 ×4 ×2 ×1 ½ ¼
weight (24) (23) (22) (21) (20) (2-1) (2-2)
position 4 3 2 1 0 -1 -2
11
Find Equivalent Decimal for Binary Numbers
• Example: Convert binary number 10011.012 to decimal
Number: 1 0 0 1 1 . 0 1 6
Position: 4 3 2 1 0 -1 -2
Weight: 24 23 22 21 20 2-1 2-2
12
Encode Decimal as Binary Numbers:
Subtraction Method (Easy for Humans)
0 0 1 1 4-4=0
32 16 8 4 2 1 DONE
0 0 1 1 0 0 answer
32 16 8 4 2 1
13
Encode Decimal in Binary Numbers:
Division Method (Good for Computers)
• Example: Convert decimal number 37 to binary
– Repeated-division-by-base (here, base 2)
remainder
2 37 1 Least Significant Bit
2 18 0 (rightmost)
2 9 1
2 4 0
2 2 0
2 Class DerivationMost Significant Bit
1 1
0
(leftmost)
(37)10 = (100101)2
14
Encode Fractional Decimal in Binary
• Example: Convert fractional part 0.71510 to binary
– Repeated-multiplication-by-base (here, base 2)
(0.715)10 (0.10110……)2
15
Encode Numbers with Bits
• More numbers need more bits to represent
– 3710 = 1001012 (6 bits)
– 13710 = 100010012 (8 bits)
– 1030710 = 101000010000112 (14 bits)
16
Encode Decimal Numbers by Binary Bits
Binary Decimal
0 0 0 0 0
0 0 0 1 1
0 0 1 0 2
0 0 1 1 3
0 1 0 0 4
0 1 0 1 5
0 1 1 0 6
0 1 1 1 7
…… 1 0 0 0 8
1 0 0 1 9
1 0 1 0 10
1 0 1 1 11
1 1 0 0 12
1 1 0 1 13
1 1 1 0 14
1 1 1 1 15
16
……
…… 17
Hexadecimal System
• The Hexadecimal system is a base 16 (modulo 16)
number system:
– 16 digits: 0 1 2 3 4 5 6 7 8 9 A B C D E F
• Letters A ~ F represent decimal 10 through decimal 15
• Each position has a decimal weight in power of 16, e.g.
E3A
E 3 A 6
×256 ×16 ×1
weight (162) (161) (160)
(E3A)16 = E×256 + 3×16 + A×1 = 3584 + 48 + 10 = 3642
Class Derivation
18
Encode Decimal to Hexadecimal
• Example: Convert decimal number 58 to hexadecimal
– Repeated-division-by-base (here, base 16)
(58)10 = (3A)16
19
Summary
Binary Decimal Hexaecimal
0 0 0 0 0 0
0 0 0 1 1 1
0 0 1 0 2 2
0 0 1 1 3 3
0 1 0 0 4 4
0 1 0 1 5 5
0 1 1 0 6 6
0 1 1 1 7 7
1 0 0 0 8 8
1 0 0 1 9 9
1 0 1 0 10 A
1 0 1 1 11 b
1 1 0 0 12 C
1 1 0 1 13 d
1 1 1 0 14 E
1 1 1 1 15 F
20
Convert Binary to Hexadecimal
• Look for groups of 4 bits starting from the LSB
• Example: Convert 11 1011 0101.11 to hexadecimal:
11 1011 0101.11 = (0011 1011 0101.1100)2
3 b 5 c
Binary Decimal Hexaecimal
0 0 0 0 0 0
(11 1011 0101.11)2 = (3b5.c)16
Class Derivation 0
0
0
0 0
0 1
0 1
1
0
1
1
2
3
1
2
3
0 1 0 0 4 4
0 1 0 1 5 5
0 1 1 0 6 6
0 1 1 1 7 7
1 0 0 0 8 8
1 0 0 1 9 9
1 0 1 0 10 A
1 0 1 1 11 b
1 1 0 0 12 C
1 1 0 1 13 d
1 1 1 0 14 E21
1 1 1 1 15 F
Convert Hexadecimal to Binary
• Each digit is converted to 4 bits in binary
• Arrange the groups of 4 bits in the same order
• Example: convert (3F7)16 to binary:
(3 F 7)16 Binary Decimal Hexaecimal
0 0 0 0 0 0
0 0 0 1 1 1
0 0 1 0 2 2
0 0 1 1 3 3
( 0011 1111 0111 )2 0 1 0 0 4 4
Class Derivation
0 1 0 1 5 5
0 1 1 0 6 6
0 1 1 1 7 7
• Drop the initial 0’s to simplify 1 0 0 0 8 8
1 0 0 1 9 9
(3F7)16 = (11 1111 0111)2 1
1
0
0
1
1
0
1
10
11
A
b
1 1 0 0 12 C
1 1 0 1 13 d
1 1 1 0 14 E
1 1 1 1 15 F
22
Octal System
• The Octal number system is a base 8 (modulo 8) number
system:
– 8 digits: 0 1 2 3 4 5 6 7
• Each position has a decimal weight in power of 8
• Each octal digital corresponds to a 3-bit binary number
(3 1 7)8
23
Convert Base-M System to Base-N System
• Decimal can always be used as the intermediate number
system
• Generally, the rule “divide/multiply by the base of
destination system” applies to all the number system
conversions
– Example, to convert a Hex number to base-3 number, just divide
the Hex number by 3
24
Binary and Hexadecimal Addition
25
How to Represent Text with Bits?
• A popular code: ASCII Symbol Encoding Symbol Encoding
REST
26
ASCII Coding Chart
27
How to Represent Signed Numbers with Bits?
• Cannot use minus sign, binary systems work with only two
values: 0 and 1
• The left-most bit of a binary number represents the sign of
a number – sign bit
– Sign bit 0 indicates positive numbers
– Sign bit 1 indicates negative number
28
Representation of Negative Numbers
• Negative numbers are represented by
– Sign and magnitude
– 1’s complement code
– 2’s complement code
• Sign and magnitude
– MSB is the sign bit: 0 positive, 1 negative
• 1’s complement representation of –N
– Negation of every bit of N
– Example, 1’s complement representation of -3
• N = 3 = 0011
Class Derivation
• -N = -3 = 1100
• 2’s complement representation of –N is
– Negation of every bit of N, then plus 1
– Example, 2’s complement representation of -3
• N = 3 = 0011
Class Derivation
• -N = -3 = 1100 + 1 = 1101
29
Signed 2’s Complement Number
• Signed numbers are represented as 2’s complement
numbers in computers
• Recognize a signed 2’s complement number
– Sign bit = 0, positive number, recognize as a regular binary number
• 0101 = +5;
– Sign bit = 1, negative number, the magnitude of the number is
obtained by 2’s complement operation
• 1011
– Sign: negative number
– Magnitude: 2’s complete operation of (1011) = 0100+1 = 0101 = 5
– So 1011 = 5
• Overflow
– If an n-bit 2’s complement number is greater than 2n-1-1 or less than 2n-1,
we say there is an overflow
34
Detecting Overflow: Method 1
• Overflow detection logic
– Two numbers’ sign bits are the same but are different from the result’s sign
bit
– If the two numbers’ sign bits are different, overflow is impossible
• Adding a positive and negative can’t exceed largest magnitude positive or
negative
• 4-bit examples
sign bits
0 1 1 1 1 1 1 1 1 0 0 0
+0 0 0 1 +1 0 0 0 +0 1 1 1
1 0 0 0 0 1 1 1 1 1 1 1
overflow overflow no overflow
(a) (b) (c)
35
Detecting Overflow - Method 2
• Simpler method: Detect difference between carry-in to sign bit and
carry-out from sign bit
c4 ≠ c3
0 1 1 1 1 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 0 0 0
+0 0 0 1 +1 0 0 0 +0 1 1 1
1 0 0 0 0 1 1 1 1 1 1 1
overflow overflow no overflow
(a) (b) (c)
• If the carry into the sign bit column differs from the
carry out of that column, overflow has occurred.
• When overflow occurs, one more bit (carry out) is needed.
36