Digital Logic Design and Application (数字逻辑设计及应用) 数字逻辑设计及应用 Chapter 2 Number Systems and codes (数系与编码) 介绍在数字逻辑体系中信号的表达方式、 类型,不同表达方式之间的转换,运算的规则 等。 1 Digital Logic Design and Application (数字逻辑设计及应用) Review of Chapter 1 (第一章内容回顾) Analog Digital versus Digital (模拟与数字) Devices (数字器件): Gates(门电路)、 Flip-flops(触发器) Electronic and Software Aspects of Digital Design (数字设计的电子技术和软件技术) 2 Digital Logic Design and Application (数字逻辑设计及应用) Review of Chapter 1 (第一章内容回顾) Integrated Circuit(IC,集成电路) Logic Devices(PLA、PLD、 CPLD、FPGA, 可编程逻辑器件) Programmable Application-Specific ICs(ASIC, 专用集成电路 ) 3 Printed-Circuit Boards (PCB, 印制电路板) Digital Logic Design and Application (数字逻辑设计及应用) Chapter 2 Number Systems and codes (数系与编码) Two kinds of Information (信息主要有两类): Numeric Data (数值信息) — Number Systems and their Conversions (数制及其转换) Nonnumeric Data (非数值信息) -- Nonnumeric Data Representation – Codes (非数值信息的表征 -- 编码) 4 Digital Logic Design and Application (数字逻辑设计及应用) Chapter 2 Number Systems and codes (数系与编码) 数字系统只处理数字信号 0 , 1; 需要将任意信息用( 0 ,1 )表达; 用(0,1)表达数量: 数制 二进制 用(0,1)表达不同对象: 符号编码 5 How to Encode Text: ASCII, Unicode ASCII: 7- (or 8-) bit encoding of each letter, number, or symbol Encoding Symbol Sample ASCII encodings 010 0000 <space>␠ Encoding Symbol 010 0001 ! 100 0001 A 010 0010 " 100 0010 B 010 0011 # 100 0011 C 010 0100 $ 100 0100 D 010 0101 % 100 0101 E 010 0110 & 100 0110 F 010 0111 ' 100 0111 G 010 1000 ( 100 1000 H 010 1001 ) 100 1001 I 010 1010 * 100 1010 J 010 1011 + 100 1011 K 010 1100 , 100 1100 L 010 1101 100 1101 M 010 1110 . 010 1111 / 6 Encoding Symbol 100 1110 N 100 1111 O 101 0000 P 101 0001 Q 101 0010 R 101 0011 S 101 0100 T 101 0101 U 101 0110 V 101 0111 W 101 1000 X … 101 1001 Y 101 1010 Z Encoding Symbol 110 0001 110 0010 ... 111 1001 111 1010 a b 011 0000 011 0001 011 0010 011 0011 011 0100 011 0101 011 0110 … 011 0111 011 1000 011 1001 0 1 2 3 4 5 6 7 8 9 y z 6 How to Encode Text: ASCII, Unicode Unicode: Increasingly popular 16-bit encoding Encodes characters from various world languages …… 7 7 How to Encode Numbers: Binary Numbers • Each position represents a base quantity; symbol in position means how many of that quantity Base ten (decimal) symbols: 0, 1, 2, ..., 8, and 9 More than 9 -- next position 5 2 3 Ten 10 4 10 3 10 2 10 1 10 0 So each position power (幂) of 10 Nothing special about base 10 -- used because we have 10 fingers 8 8 How to Encode Numbers: Binary Numbers • Each position represents a base quantity; symbol in position means how many of that quantity Base two (binary) Two symbols: 0 and 1 More than 1 -- next position So each position power (幂) of 2 9 1 0 1 Q: How much? 24 23 22 21 20 + = a 4 + 1 = 5 There are only 10 types of people in the world: those who understand binary, and those who don't. 9 Useful to know powers of 2: 29 28 27 26 25 24 23 22 21 20 512 256 128 64 32 16 8 4 2 1 Practice counting up by powers of 2: 512 256 128 64 32 16 8 4 2 1 10 10 Digital Logic Design and Application (数字逻辑设计及应用) 2.1 Positional Number System (按位计数制) Any Decimal Number D Can Be Represented as the Following (任意十进制数 D 可表示如下): D = dp-1 dp-2 ... d1 d0 . d-1 d-2 ... d-n p 1 d i n i r i Weight of i bit; Base or Radix of r Number System (第i位的权; r 计数制的基数) 11 推广: D2 = ∑ d i × 2i D16= ∑ d i × 16i Digital Logic Design and Application (数字逻辑设计及应用) 2.1 Positional Number System (按位计数制) 按位计数制的特点 1) 采用基数(Base or Radix), R进制的基数是R 2) 基数确定数符的个数 如十进制的数符为:0、1、2、3、4 、5、6、7、8、9,个数为10 二进制的数符为:0、1,个数为2 3)逢基数进一 12 Digital Logic Design and Application (数字逻辑设计及应用) 2.1 Positional Number System (按位计数制) Decimal and Binary Decimal system: base is 10, the digit may be 0 to 9 101.110 110 2 0 10 110 110 1 0 1 Binary system: base is 2, the digit may be 0 or 1 101.12 1 22 0 21 1 20 1 21 13 bit: one digit in binary system; Using Digital Data in a Digital System A temperature sensor outputs temperature in binary The system reads the temperature, outputs ASCII code: “F” for freezing (0-32) “B” for boiling (212 or more) “N” for normal temperature sensor 0 0 1 0 0 0 0 1 "33" 14 14 Using Digital Data in a Digital System A display converts its ASCII input to the corresponding letter temperature sensor 0 0 1 0 0 0 0 1 "33" Digital System if (input <= "00100000") // "32" output = "1000110" // "F" else if (input >= "11010100") // "212" output = "1000010" // "B" else output = "1001110" // "N" 1 0 0 1 1 1 0 display "N" N 15 15 Digital Logic Design and Application (数字逻辑设计及应用) 2.2 Octal and Hexadecimal Numbers (八进制和十六进制) 数码 特性 8 0~7 逢八进一 2 0,1 逢二进一 基数 Octal Number (八进制) Binary Number (二进制) Hexadecimal Number(十六进制) 16 16 0~9,A~F 逢十六进一 Digital Logic Design and Application (数字逻辑设计及应用) 说 明 选择什么数制来表示信息, 对数字系统的成本和性能影响很大, 在数字电路中多使用二进制. Significant Bit(MSB, 最高有效位) Least Significant Bit(LSB, 最低有效位) Most 1011100010112 MSB 17 LSB Digital Logic Design and Application (数字逻辑设计及应用) 表2.1 十进制、二进制、八进制与十六进制数 18 十进制 二进制 八进制 十六进制 0 1 2 0000 0001 0010 0 1 2 0 1 2 3 4 5 0011 0100 0101 3 4 5 3 4 5 6 7 0110 0111 6 7 6 7 8 9 10 1000 1001 1010 10 11 12 8 9 A Base Sixteen: Another Base Used by Designers Nice because each position represents four base-two positions Compact way to write binary numbers Known as hexadecimal, or just hex Q: Write 11110000 in hex A: F0 Q: Convert hex A01 to binary A: 1010 0000 0001 19 19 Digital Logic Design and Application (数字逻辑设计及应用) 二进制与八进制和十六进制之间的转换 位数替换法:保持小数点不变,每位八进制数对 应3位二进制数; 每位十六进制数对应4位二进制数; 二进制转换时,从小数点开始向左右分组,在MSB前 面和LSB后面可以加0; 转换为二进制时,MSB前面和LSB后面的0不写; 例:1011100010112=56138=B8B16 10111000.10112=207.548=B8.B16 20 1000110010012 = ( = ( )8 )16 Hex Example: RFID Tag Batteryless(无电池) tag powered(功率 ) by radio field Transmits(发送) unique identification( 鉴定) number Example: 32 bit id(身份证明) 8-bit province number, 8-bit city number, 16-bit animal number Tag contents are in binary But programmers use hex when writing/reading (b) Province # (c) Province: 7 City: 160 (d) 00000111 10100000 (e) 07 A0 (a) RFID tag 21 City # (f) Animal # Animal : 513 00000010 00000001 02 01 Tag ID in hex: 07A00201 21 Digital Logic Design and Application (数字逻辑设计及应用) 2.3 General Positional-Number-System Conversion (常用按位计数制的转换) A Number in any Radix to Radix 10 (任意进制数 十进制数) Method: Expanding the formula using radix-10 arithmetic (方法:利用位权展开) Example 1:( 101.01 )2 = ( ( 7F.8 )16 = ( )10 )10 More easy way(更简便的方法)? 22 ( F1AC )16 = ( ( ( F16 ) +1 ) 16 + A ) 16 + C Digital Logic Design and Application (数字逻辑设计及应用) 2.3 General Positional-Number-System Conversion (常用按位计数制的转换) A Number in Radix 10 to any Radix (十进制 其它进制) Method:Radix Multiplication or Division ( 基数乘除法) Integer Parts (整数部分): 除 r 取余,逆序排列 Example 2:( 156 )10 = ( )2 Decimal Fraction Parts (小数部分): 乘 r 取整,顺序排列 Example 3:( 0.37 )10 = ( )2 23 Digital Logic Design and Application (数字逻辑设计及应用) 2.3 General Positional-Number-System Conversion (常用按位计数制的转换) Example 4:Require < 10-2 ,Complete the following conversion ( 617.28 )10 = ( 2-n <= < 10-2 )2 n=7 思考:任意两种进位计数制之间的转换 以十进制(二进制)作为桥梁 24 Converting To/From Binary by Hand: Summary 25 25 Divide-By-2 Method Common in Automatic Conversion Repeatedly divide decimal number by 2, place remainder in current binary digit (starting from 1s column) 26 1. Divide decimal number by 2 Insert remainder into the binary number Continue since quotient (6) is greater than 0 Decimal 6 2 12 –12 0 Binary 0 1 (current value: 0) 2. Divide quotient by 2 Insert remainder into the binary number Continue since quotient (3) is greater than 0 3 2 6 –6 0 0 0 2 1 (current value: 0) 3. Divide quotient by 2 Insert remainder into the binary number Continue since quotient (1) is greater than 0 1 2 3 –2 1 1 0 0 4 2 1 (current value: 4) 4. Divide quotient by 2 Insert remainder into the binary number Quotient is 0, done 0 2 1 –0 1 1 1 0 0 8 4 2 1 (current value: 12) Note: Works for any base N—just divide by N instead 26 Bytes, Kilobytes, Megabytes, and More Byte: 8 bits Common metric prefixes: (thousand, or 103), mega (million, or 106), giga (billion, 千兆or 109), and tera (trillion, 万亿or 1012), e.g., kilobyte, or KByte kilo 27 27 Bytes, Kilobytes, Megabytes, and More BUT, metric prefixes also commonly used inaccurately = 65536 commonly written as “64 Kbyte” Typical when describing memory sizes 216 watch out for “KB” for kilobyte vs. “Kb” for kilobit Also 28 28 Example: DIP-Switch Controlled Channel Ceiling fan receiver should be set in factory to respond to channel “73” Convert 73 to binary, set DIP(指拨) switch accordingly Desired value: 73 01001001 (a) Q : 128 64 32 16 8 4 2 1 sum: 64 72 (b) 73 29 29 Example: DIP-Switch Controlled Channel DIP switch 1 0 channel receiver "34" 0 0 1 0 0 0 1 0 "73" 0 1 0 0 1 0 0 1 InA if (InA = InB) Out = 1 else Ceiling fan Out = 0 module Out 0 30 InB (c) Digital Logic Design and Application (数字逻辑设计及应用) 2.4 Addition and Subtraction of Nondecimal Numbers (非十进制数的加法和减法) Two Binary Number Arithmetic (两个二进制数的算术运算) Addition (加法): Carry (进位) 1 + 1 = 10 Subtraction (减法): Borrows (借位) 31 10 – 1 = 1 Digital Logic Design and Application (数字逻辑设计及应用) 2.4 Addition and Subtraction of Non-decimal Numbers (非十进制数的加法和减法) •Carry in (进位输入): C in (P.32) • Carry out ( 进位输出 ) C out Sum ( 本位和 ): S 1011 1110 + 1000 1101 32 Digital Logic Design and Application (数字逻辑设计及应用) 表2.3.1 二进制加法真值表 输 入 33 输 出 被加数X 加数Y 输入进位Cin 和S 进位输出Cout 0 0 0 0 1 1 1 1 0 0 1 1 0 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 0 1 0 0 0 1 0 1 1 1 Adder Example: DIP-Switch-Based Adding Calculator Goal: Create calculator that adds two 8bit binary numbers, specified using DIP switches DIP switch: Dual-In-line Package switch, move each switch up or down Solution: Use 8-bit adder 34 34 Adder Example: DIP-Switch-Based Adding Calculator Solution: Use 8-bit adder DIP switches 1 0 a7……..a0 b7……..b0 8-bit carry-ripple adder ci co s7…………s0 0 CALC LEDs 35 Adder Example: DIP-Switch-Based Adding Calculator To prevent spurious(假的) values from appearing at output, can place register at output Actually, the light flickers(闪烁) from spurious values would be too fast for humans to detect —but the principle of registering outputs to avoid spurious values being read by external devices (which normally aren’t humans) applies here. 36 36 Adder Example: DIP-Switch-Based Adding Calculator DIP switches 1 0 a7…….a0 b7……b0 ci 0 8-bit adder co s7………s0 e clk ld 8-bit register CALC LEDs 37 Digital Logic Design and Application (数字逻辑设计及应用) 2.4 Addition and Subtraction of Non-decimal Numbers (非十进制数的加法和减法) •Borrow in ( 借位输入 ): Bin •Borrow out ( 借位输出 ): Bout •Difference bit ( 本位差 ): D 1010 1010 – 0101 0101 38 Digital Logic Design and Application (数字逻辑设计及应用) 表2.3.2 二进制减法真值表 输 入 39 输 出 被减数X 减数Y 输入借位Bin 差D 输出借位Bout 0 0 0 0 0 0 0 1 1 1 0 1 0 1 1 0 1 1 0 1 1 0 0 1 0 1 0 1 0 0 1 1 0 0 0 1 1 1 1 1 Subtractor Example: DIP-Switch Based Adding/Subtracting Calculator Extend earlier calculator example Switch f indicates whether want to add (f=0) or subtract (f=1) Use subtractor and 2x1 mux 40 Subtractor Example: DIP-Switch Based Adding/Subtracting Calculator DIP switches 1 0 8 1 0 clk f e 8 A B ci 8-bit adder co S 8 ld 8 8 0 A B wi 8-bit subtractor wo S 0 8 0 2x1 1 8 8-bit register CALC 8 LEDs 41 Digital Logic Design and Application (数字逻辑设计及应用) 2.5 Representation of Negative Numbers (负数的表示) 2.5.1 Signed-Magnitude Representation [符号 – 数值表示法(原码)] MSB as the Sign bit (0 = plus, 1 = minus) [最高有效位表示符号位( 0 = 正,1 = 负)] 01111111=+127 00101110=+46 00000000=+0 42 11111111=-127 10101110=-46 10000000=-0 Digital Logic Design and Application (数字逻辑设计及应用) 2.5 Representation of Negative Numbers (负数的表示) 2.5.1 Signed-Magnitude Representation [符号 – 数值表示法(原码)] Two possible representations of Zero [零有两种表示(+ 0、 – 0)] An n-bit signed-magnitude integer range is (n位二进制整数表示范围): – ( 2n-1 – 1) ~ + ( 2n-1 – 1) 43 Digital Logic Design and Application (数字逻辑设计及应用) 2.5 Representation of Negative Numbers (负数的表示) 2.5.2 Complement Number Systems (补码数制) – Complement (基数补码) Diminished Radix – Complement [ 基数减1补码 (反码) ] Radix 44 Digital Logic Design and Application (数字逻辑设计及应用) 2.5 Representation of Negative Numbers (负数的表示) 2.5.3 Radix – Complement Representation ( 基数补码表示法) The complement of an n-digit number is obtained by subtracting it from r n (n位数的补码等于从 r n 中减去该数) Example 45 : Table 2-4 P.36 Digital Logic Design and Application (数字逻辑设计及应用) 2.5 Representation of Negative Numbers (负数的表示) Radix – Complement Representation [ 基数减1补码表示法(反码)]: The Diminished Radix – Complement of an n-digit number is obtained by subtracting it from r n -1 [ n位数的反码等于从 r n – 1 中减去该数] Diminished Example 46 : Table 2-4 2-5 P.36 Ten’s Complement Before introducing two’s complement, let’s consider ten’s complement But, be aware that computers DO NOT USE TEN’S COMPLEMENT. Introduced for intuition(直觉) only. Complements for each base ten number shown to right. Complement is the number that when added results in 10 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 47 47 Ten’s Complement Nice feature of ten’s complement Instead of subtracting a number, adding its complement results in answer exactly 10 too much So just drop(丢下) the 1 – results in subtracting using addition only complements 1 9 2 8 3 7 4 6 5 5 6 4 7 3 8 2 9 1 0 48 10 10 4 6 7 0 3 –4 10 +6 20 13 13 3 7–4=3 7+6=13 3 Adding the complement results in an answer that is exactly 10 too much – dropping the tens column gives the right answer. 48 Digital Logic Design and Application (数字逻辑设计及应用) 2.5.4 Two’s – Complement Representation (二进制补码表示法) The MSB of a number in this system serves as the sign bit(最高有效位用做符 号位); The weight of the MSB is -2n-1 instead of + 2n1 (MSB的权是-2n-1 而不是+ 2n-1 ) Obtain a Two’s- Complement ( 二进制补码的求取 ): Ones’ – Complement (反码) + 1 (Why??) 49 Digital Logic Design and Application (数字逻辑设计及应用) 2.5.4 Two’s – Complement Representation (二进制补码表示法) 例2.5.1 若约定字长是一个字节,试求-119的 补码。 解:因-119的绝对值119=01110111,则补码 可以通过下式算法得到: 全1码: 11111111 减去-119绝对值: 0 1 1 1 0 1 1 1 -119的反码: 10001000 加1: 1 -119补码: 10001001 50 Digital Logic Design and Application (数字逻辑设计及应用) 2.5.4 Two’s – Complement Representation (二进制补码表示法) Only one representations of Zero ( 零只有一种表示 ) 逐位取反 约定8位 51 0=0 0 0 0 0 0 0 0 11111111 +1 0 0 0 0 0 0 0 0=0 Digital Logic Design and Application (数字逻辑设计及应用) 2.5.4 Two’s – Complement Representation (二进制补码表示法) An n-bit Two’s- Complement range is (n位二进制补码表示范围): – 2 n-1 ~ + ( 2 n-1 – 1) 约定字长(8比特)后,补码表示数的范围 -128~127 52 Digital Logic Design and Application (数字逻辑设计及应用) 2.5.4 Two’s – Complement Representation (二进制补码表示法) Positive number has the same: Sign-Magnitude, Ones’ – Complement, and Two’s- Complement ( 正数的原码、反码、补码相同) 53 54 十进制 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 表2-6 1位十进制数与4位二进制数 二进制原码 二进制反码 二进制补码 —— —— 1000 1111 1000 1001 1110 1001 1010 1101 1010 1011 1100 1011 1100 1011 1100 1101 1010 1101 1110 1001 1110 1111 0000 1000或0000 1111或0000 0001 0001 0001 0010 0010 0010 0011 0011 0011 0100 0100 0100 0101 0101 0101 0110 0110 0110 0111 0111 0111 Digital Logic Design and Application (数字逻辑设计及应用) 2.5 Representation of Negative Numbers (负数的表示) 2.5.6 Ones’ – Complement Representation (二进制反码表示法) The Sign Bit doesn’t change, Other Bits converse based on the SignedMagnitude. (符号位不变,其余在原码基础上按位取反) 55 Representing Negative Numbers: Two’s Complement Big advantage: Allows us to perform subtraction using addition Thus, only need adder component, no need for separate subtractor component 56 56 Two’s Complement Subtractor Built with an Adder Using two’s complement A – B = A + (-B) = A + (two’s complement of B) = A + invert_bits(B) + 1 So build subtractor using adder by inverting B’s bits, and setting carry in to 1 57 Two’s Complement Subtractor Built with an Adder A B N-bit A B Adder S 58 1 Cin Digital Logic Design and Application (数字逻辑设计及应用) Summary of this class (本堂课小结) Positional Number System (按位计数制) D p 1 i d r i i n Binary, Octal, and Hexadecimal Numbers ( 二进制、八进制、十六进制) 59 Digital Logic Design and Application (数字逻辑设计及应用) Summary of this class (本堂课小结) General Positional-Number-System Conversion (常用按位计数制的转换) A Number in any Radix to Radix 10 : Expanding the formula using radix-10 arithmetic (任意进制数 十进制数:利用位权展开) 60 Digital Logic Design and Application (数字逻辑设计及应用) Summary of this class (本堂课小结) General Positional-Number-System Conversion (常用按位计数制的转换) A Number in Radix 10 to any Radix : Radix Multiplication or Division (十进制 其它进制:基数乘除法) Note: Decimal Fraction Parts Conversion [ 注意:小数部分的转换(误差)] 61 Digital Logic Design and Application (数字逻辑设计及应用) Summary of this class (本堂课小结) Addition and Subtraction of Nondecimal Numbers (非十进制的加法和减法) (Table 2-3) 62 进位输入 Cin 、进位输出 Cout 、 本位和 S 借位输入 Bin 、借位输出 Bout 、 本位差 D Digital Logic Design and Application (数字逻辑设计及应用) Summary of this class (本堂课小结) Representation of Negative Numbers (负数的表示) Signed-Magnitude [ 符号-数值(原码)] Complement Number Systems (补码数制) Radix – Complement (基数补码) Diminished Radix – Complement [ 基数减1补码(基数反码)] 63 Digital Logic Design and Application (数字逻辑设计及应用) Summary of this class (本堂课小结) Binary Signed-Magnitude, Ones’ – Complement, and Two’s – Complement Representation (二进制的原码、反码、补码) 正数的原码、反码、补码表示相同 负数的原码表示:符号位为 1 负数的反码表示: 符号位不变,其余在原码基础上按位取反 在 |D| 的原码基础上按位取反(包括符号位) 负数的补码表示:反码 64 +1 Digital Logic Design and Application (数字逻辑设计及应用) 第2章作业(P50~52) 2.1 (e) (i) 2.2 (e) 2.3 (e) 2.4 2.5 (e) (j) 2.6 (f) 补充: (125.17)10=?2 2.7 (a) 65 2.8 (a) 2.9 (b) 2.10 (c) 2.11 +25 – 42 2.18 (b) (d) 2.19 Digital Logic Design and Application (数字逻辑设计及应用) A Class Problem Write the 8-bit Signed-magnitude, Two’s-complement, and One’scomplement for each of these decimal numbers: +115 66 -100 Digital Logic Design and Application (数字逻辑设计及应用) Chapter 2 Number Systems and codes (数系与编码) Numeric Data- Number Systems and their Conversions (数值信息 —— 数制及其转换) Nonnumeric Data- Codes (非数值信息 —— 编码) 67 Digital Logic Design and Application (数字逻辑设计及应用) Review of Chapter 2 (第二章内容回顾) Positional Number System (按位计数制) D p 1 i d r i i n Binary, Octal, and Hexadecimal Numbers ( 二进制、八进制、十六进制) 68 Digital Logic Design and Application (数字逻辑设计及应用) Review of Chapter 2 (第二章内容回顾) General Positional-Number-System Conversion (常用按位计数制的转换) A Number in any Radix to Radix 10 : Expanding the formula using radix-10 arithmetic (任意进制数 十进制数:利用位权展开) 69 Digital Logic Design and Application (数字逻辑设计及应用) Review of Chapter 2 (第二章内容回顾) General Positional-Number-System Conversion (常用按位计数制的转换) A Number in Radix 10 to any Radix : Radix Multiplication or Division (十进制 其它进制:基数乘除法) Note: Decimal Fraction Parts Conversion [ 注意:小数部分的转换(误差)] 70 Digital Logic Design and Application (数字逻辑设计及应用) Review of Chapter 2 (第二章内容回顾) Addition and Subtraction of Nondecimal Numbers (非十进制的加法和减法) (Table 2-3) 71 进位输入 Cin 、进位输出 Cout 、 本位和 S 借位输入 Bin 、借位输出 Bout 、 本位差 D Digital Logic Design and Application (数字逻辑设计及应用) Review of Chapter 2 (第二章内容回顾) Representation of Negative Numbers (负数的表示) Signed-Magnitude [ 符号-数值(原码)] Complement Number Systems (补码数制) Radix – Complement (基数补码) Diminished Radix – Complement [ 基数减1补码(基数反码)] 72 Digital Logic Design and Application (数字逻辑设计及应用) Review of Chapter 2 (第二章内容回顾) Binary Signed-Magnitude, Ones’ – Complement, and Two’s – Complement Representation (二进制的原码、反码、补码) 正数的原码、反码、补码表示相同 负数的原码表示:符号位为 1 负数的反码表示: 符号位不变,其余在原码基础上按位取反 在 |D| 的原码基础上按位取反(包括符号位) 负数的补码表示:反码 73 +1 Digital Logic Design and Application (数字逻辑设计及应用) 2.5.4 Two’s – Complement Representation (二进制补码表示法) An n-bit Two’s- Complement range is (n位二进制补码表示范围): – 2 n-1 ~ + ( 2 n-1 – 1) Only one representations of Zero ( 零只有一种表示 ) Obtain a Two’s- Complement ( 二进制补码的求取 ): Ones’ – Complement (反码) + 1 (为什么??) Expanding the Sign Bit ( 符号位扩展 ) 74 Digital Logic Design and Application (数字逻辑设计及应用) 2.5 Representation of Negative Numbers (负数的表示) Example 2.5.2:Write the 8-bit signedmagnitude, two’s-complement for each of these binary numbers. (分别写出下面二进制数的8位符号–数值码、补 码) ( – 1101 )2 ( – 0 . 1101 )2 75 Digital Logic Design and Application (数字逻辑设计及应用) 2.5 Representation of Negative Numbers (负数的表示) 1、( – 1101 )2 2、( – 0 . 1101 )2 [ [ D ] 反 ]反 = D 1、5位二进制表示: 原码 反码 补码 1 1101 1 0010 1 0011 [ [ D ] 补 ]补 = D 76 2、8位二进制表示: 原码 反码 补码 1000 1101 1111 0010 1111 0011 Digital Logic Design and Application (数字逻辑设计及应用) 2.6 Two’s – Complement Addition and Subtraction (二进制补码的加法和减法) Addition Rules: Added by ordinary binary addition (加法:按普通二进制加法相加)P.39 Subtraction Rules: Taking its two’s complement, then add (减法:将减数求补,再相加) 77 Digital Logic Design and Application (数字逻辑设计及应用) 2.6 Two’s – Complement Addition and Subtraction (二进制补码的加法和减法) +2 + +5 +7 0010 + 0101 0111 -3 1101 + -5 + 1011 -8 11000 +7 + -4 +3 0111 + 1100 10011 +1 0001 + -6 + 1010 -5 1011 78 Adder/Subtractor Example: Calculator Previous calculator used separate adder and subtractor DIP switches 1 0 8 A 1 0 clk 8 B 8-bit adder co S f 8 e ld 8 ci 0 0 8 A B wi 0 8-bit subt ractor wo S 2x 1 8 1 8 8-bit register CALC 8 LEDs 79 79 Adder/Subtractor Example: Calculator Improve by using adder/subtractor, and two’s complement numbers DIP switches 1 0 8 f 1 0 e clk 8 A B sub 8-bit adder/subtractor S 8 ld 8-bit register CALC 8 LEDs 80 80 Digital Logic Design and Application (数字逻辑设计及应用) 2.6 Two’s – Complement Addition and Subtraction (二进制补码的加法和减法) Overflow(溢出) 如果加法运算产生的和超出了数制表示的范围,则结果 发生了溢出(Overflow)。 对于二进制补码,加数的符号相同,和的符号与加数的符号 不同。(或者,C in 与 C out 不同) P.41 对于无符号二进制数,若最高有效位上发生进位或借位,就 指示结果超出范围。 -5 1011 +7 0111 + -6 + 1010 + +3 + 0011 -11 10101 = +5 +10 1010 = -6 81 Overflow Sometimes result can’t be represented with given number of bits Either 82 too large magnitude of positive or negative Ex. 4-bit two’s complement addition of 0111+0001 (7+1=8). But 4-bit two’s complement can’t represent number >7 0111+0001 = 1000 WRONG answer, 1000 in two’s complement is -8, not +8 Adder/subtractor should indicate when overflow has occurred, so result can be 82 discarded Detecting Overflow: Method 1 For two’s complement numbers, overflow occurs when the two numbers’ sign bits are the same but differ from the result’s sign bit If the two numbers’ sign bits are initially different, overflow is impossible Adding positive and negative can’t exceed largest magnitude positive or negative 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) If the numbers’ sign bits have the same value, which differs from the result’s sign bit, overflow has occurred.83 83 Detecting Overflow: Method 2 Even simpler method: Detect difference between carry-in to sign bit and carry-out from sign bit 000 000 111 0111 1111 1000 +0 0 0 1 +1 0 0 0 +0 1 1 1 0 1 0 0 0 10 1 1 1 01 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. 84 84 Digital Logic Design and Application (数字逻辑设计及应用) 2.10 Binary Codes for Decimal Numbers (十进制数的二进制编码) A set of n-bit strings in which different bit strings Represent different numbers or other things. (用于表示不同数或其它事件的一组n位二进制码的集合) 85 Digital Logic Design and Application (数字逻辑设计及应用) 2.10 Binary Codes for Decimal Numbers (十进制数的二进制编码) How to represent a 1-bit Decimal number with a 4-bit Binary code (如何用 4位二进制码 表示 1位十进制码)? —— 86 Binary Coded Decimal (BCD码) Digital Logic Design and Application (数字逻辑设计及应用) 2.10 Binary Codes for Decimal Numbers (十进制数的二进制编码) 87 How to represent a Negative BCD number (负的BCD数如何表示)? Signed-Magnitude Representation: Encoding of the sign bit is arbitrary (符号-数值表示:符号位的编码任意) 10’s-complement: 0000 indicates plus, 1001 indicates minus. (十进制补码表示:0000正,1001负) Addition of BCD Digits (BCD数的加法) P.50 Digital Logic Design and Application (数字逻辑设计及应用) 0101 5 + 9 + 1001 14 1110 + 0110 10 + 4 1 0100 8 1000 + 8 + 1000 16 1 0000 + 0110 10 + 6 1 0110 88 0100 4 + 5 + 0101 9 1001 修正 修正 9 1001 + 9 + 1001 18 1 0010 + 0110 10 + 8 1 1000 修正 Digital Logic Design and Application (数字逻辑设计及应用) 2.10 Binary Codes for Decimal Numbers (十进制数的二进制编码) (Table 2-9) BCD Code Weighted Code (加权码) 2421 Code Self-Complement Code自反码 Excess-3 (余3码) Biquinary Code (二五混合码) 1-out-of-10 (10中取1码) 89 Digital Logic Design and Application (数字逻辑设计及应用) 十进制数字 BCD(8421)码 2421码 余3码 二五混合码 10中取1码 0 1 2 3 4 5 6 7 8 9 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 0000 0001 0010 0011 0100 1011 1100 1101 1110 1111 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 0100001 0100010 0100100 0101000 0110000 1000001 1000010 1000100 1001000 1010000 1000000000 0100000000 0010000000 0001000000 0000100000 0000010000 0000001000 0000000100 0000000010 0000000001 0000000 0000001 0000010 0000011 0000101 … 0000000000 0000000011 0000000101 0000000110 0000000111 … 未用的码字 1010 1011 1100 1101 1110 1111 90 0101 0110 0111 1000 1001 1010 0000 0001 0010 1101 1110 1111 BCD codes 8421 code Natural code , just like 4-bit binary numbers; Each digit is weighted; It has 10 valid code words and 6 invalid code words. 91 BCD codes 2421 codes Each digit is weighted; Self-complementing; Use MSB to express higher/lower part; It has 10 valid codes and 6 invalid codes. 92 BCD codes Excess-3 code Its digit is not weighted; 8421 code + “0011”; Selfcomplementing . 93 BCD codes Examples: use BCD code for decimal numbers A = 1946 8421 code : A = 0001 1001 0100 0110 2421 code : A = 0001 1111 0100 1100 Excess-3 code: A = 0100 1100 0111 1001 94 One hot codes 1-out-of-10 code One hot code: It is very useful in control systems. 95 Two hot codes Biquinary code 7-bits; two hot code; First 2 bits is one hot code for higher/lower range; Last 5 bits is one hot code in the range. Error-detecting property ! 96 Temperature code From one code to its neighbor, only one bit changed, no transition state. 97 Digital Logic Design and Application (数字逻辑设计及应用) 2.11 Gray code(格雷码) 111 000 100 001 110 000 001 101 001 101 010 100 98 011 001 111 011 110 010 Digital Logic Design and Application (数字逻辑设计及应用) 2.11 Gray code(格雷码) 特点: 任意相邻码字间只有一位数位变化 最高位的0和1只改变一次 最大数回到0也只有一位码元不同 111 000 100 001 110 000 001 101 0 0 1 101 010 100 99 011 0 0 1 111 011 110 010 Digital Logic Design and Application (数字逻辑设计及应用) 2.11 Gray code(格雷码) 构造方法 直接构造 The bits of an n-bit binary cord word are numbered from right to left, from 0 to n-1. [对 n 位二进制的码字从右到左编号(0 ~ n-1)] Bit i of a Gray-code code word is 0 if bits i and i+1 of the corresponding binary code word are the same, else bit i is 1. (若二进制码字的第 i 位和第 i + 1 位相同,则对应的 格雷码码字的第 i 位为0,否则为1。) Reflected Code(反射码) 100 Gray codes Target: code for continues changed numbers (in binary system) to prevent wrong code happened in transition time; Property : In each pair of successive code words, only one bit changes. 101 Gray codes From binary number to Gray code The width is same, the MSB is same; From left to right, if a bit in binary number is same as its left bit, the gray code is 0, if it is different, the gray code is 1. Examples: 102 binary number: 1001 0010 0110 0011 Gray codes: 0101 0010 1101 1011 Error-detecting code Information word + checking bit 103 Digital Logic Design and Application (数字逻辑设计及应用) 2.12 Character Codes (字符编码) ASCII码(P36 表2-11) ASCII code:128 Keyboard signs , 7-bit Used for keyboard or display device 104 Digital Logic Design and Application (数字逻辑设计及应用) 2.13 Codes for Actions, Conditions, and States (动作、条件和状态的编码) 使用 b 位二进制编码来表示 n 个不同状态 Word: a digital string to represent an object Use n bits, we can make 2n different words; To make n words, you must use log n bits. 2 105 Digital Logic Design and Application (数字逻辑设计及应用) 2.16 Codes for Serial Data Transmission and Storage (用于串行数据传输与存储的编码) Parallel way use n-line to transmit an n-bits code words ; transmit an n-bits code words in one time period; Serial way use one line to transmit an n-bits code words ; transmit an n-bits code words in n time period; 106 Digital Logic Design and Application (数字逻辑设计及应用) 第2章 掌握重点 正数的十进制、二进制、八进制、十六 进制表示,以及它们之间的相互转换 符号数的 S-M码、补码、反码表示,以 及它们之间的相互转换; 带符号数的补码的加减运算:溢出的判断 BCD码、GRAY码:表示方法、特点 107 Digital Logic Design and Application (数字逻辑设计及应用) 第2章作业(P75~78) 2.12 (b) (c) 2.34 2.35 2.38 2.39 2.22(选做) 2.30 (选做) 2.51 (选做) 108 Digital Logic Design and Application (数字逻辑设计及应用) Chapter 3 Digital Circuits ( 数字电路) Give a knowledge of the Electrical aspects of Digital Circuits (介绍数字电路中的电气知识) 109 Digital Logic Design and Application (数字逻辑设计及应用) Consider some Questions (思考几个问题) 在模拟的世界中如何表征数字系统? 如何将物理上的实际值 映射为逻辑上的 0 和 1 ? 什么时候考虑器件的逻辑功能; 什么时候考虑器件的模拟特性? 110 Digital Logic Design and Application (数字逻辑设计及应用) A Class Problem ( 每课一题 ) Indicate whether or not overflow occurs when adding the following 8-bit two’scomplement numbers: 111 1101 0100 0010 0110 + 1010 1011 + 0101 1010