Digital Systems & Binary Codes
Digital Systems & Binary Codes
數位系統與二進位碼
             授課老師:黃俊堯
             研究室:社4F14
             E-mail: jyhuang@mail.ntpu.edu.tw
       章節
            1-1 Digital Systems
            1-2 Binary Numbers
            1-3 Number Base Conversions
            1-4 Octal and Hexadecimal Numbers
            1-5 Complements
            1-6 Signed Binary Numbers
            1-7 Binary Codes
            1-8 Binary Storage and Registers
            1-9 Binary Logics
2008/2/23                    第一章 數位系統與二進位碼      1
Digital Design, 4th Ed
     l      Handheld Calculator
                                     - Usually designed to minimize cost. “Optimized
                                       for low cost”
                                     - Of course, low cost comes at the expense of
                                       speed.
Design Representation
       Hierarchy in Designs
       l    Helps control complexity -
            l   by hiding details and reducing the total number of things to
                handle at any time.
       l    Modulalizes the design -
            l   divide and conquer
            l   simplifies implementation and debugging
       l    Top-Down Design
            l   Starts at the top (root) and works down by successive refinement.
       l    Bottom-up Design
            l   Starts at the leaves & puts pieces together to build up the design.
       l    Which is better?
            l   In practice both are needed & used.
                l    Need top-down divide and conquer to handle the complexity.
                l    Need bottom-up because in a well designed system, the structure is
                     influence by what primitives are available.
2008/2/23                                第一章 數位系統與二進位碼                                    7
Digital Design, 4th Ed
       Digital Design
   Given a functional description and performance, cost, & power constraints,
     come up with an implementation using a set of primitives.
   l    How do we learn how to do this?
        1. Learn about the primitives.
        2. Learn about design representation.
        3. Learn formal methods to optimally manipulate the representations.
        4. Look at design examples.
        5. Use trial and error - CAD tools and prototyping.
   l    Digital design is in some ways more an art than a science.
        The creative spirit is critical in combining primitive elements &
        other components in new ways to achieve a desired function.
   l    Unlike art, we have objective measures of a design:
                         performance cost power
2008/2/23                             第一章 數位系統與二進位碼                             8
       Signal
        l   An information variable represented by physical quantity
        l   For digital systems, the variable takes on discrete values
              l   Two level, or binary values are the most prevalent values
        l   Binary values are represented abstractly by:
              l    digits 0 and 1
              l    words (symbols) False (F) and True (T)
              l    words (symbols) Low (L) and High (H)
              l    and words On and Off.
        l   Binary values are represented by values or ranges of
            values of physical quantities
             … a5a4a3a2a1.a−1a−2a−3…                                                        aj
                                                          Decimal point                                   Power
            Example:
               7,329 = 7 × 103 + 3 × 102 + 2 × 101 + 9 × 100
       各種不同的數字系統
        l   數字系統的不同在於每一個數本身基底的不同,基
            底又稱為底數(base or radix)。基底的意義代表著
            此數為幾進制的意思。
        l   各種不同的數字系統要如何分辦呢?一般均會在數
            碼的右下方標示一個較小的數字,以表示幾進位的
            意思。
        l   二進制常被使用於電腦內部的數值運算,十進制為
            人類慣用的數字系統,十六進制則是人類與電腦溝
            通時二進制的簡化表示法,即為了辨認的方便性而
            將四個二進制數改寫成十六進制的一個元素。
       Binary Numbers
      Example: Base-2 number
       (11010.11)2 = (26.75)10
       = 1 × 2 4 + 1 × 2 3 + 0 × 2 2 + 1 × 21 + 0 × 20 + 1 × 2 −1 + 1 × 2 −2
      Example: Base-5 number
       (4021.2)5
       = 4 × 53 + 0 × 52 + 2 × 51 + 1 × 50 + 2 × 5−1 = (511.5)10
       Binary Numbers
       Example: Base-2 number
        (110101)2 = 32 + 16 + 4 + 1 = (53)10
     Special Powers of 2
            § 210 (1024) is Kilo, denoted "K"
             Powers of two
                          Table 1.1
2008/2/23                                 第一章 數位系統與二進位碼                        13
Digital Design, 4th Ed
Table 1.1
       二進制 ~ 十八進制間各種不同進制
       的基本元素
       二進制的運算
       l    二進制的加法運算
       l    二進制的減法運算
       l    二進制的乘法運算
            l   二進制乘法運算也只是二進制加法運算的擴充而
                已,只不過借助了一些移位暫存器來協助完成。
       l    二進制的除法運算
            l   電腦內部只有加法運算而已,若要完成除法運算就
                必須利用二進制的減法運算(減法運算也是加法運
                算搭配補數的應用),以及借助於右移位暫存器,
                借助於左移位暫存器的應用,電腦也可以執行乘法
                運算。
2008/2/23                第一章 數位系統與二進位碼         16
       常見的二進制的運算
        l Single Bit Addition with Carry
        l Multiple Bit Addition
l Multiplication
l BCD Addition
       二進制的運算範例
     l      Addition                        l   Subtraction
            Augend:      101101                 Minuend:      101101
            Addend: +100111                     Subtrahend: −100111
     l      Multiplication
            Multiplicand              1011
            Multiplier               × 101
            Partial Products          1011
                                     0000 -
                                    1011 - -
            Product                 110111
2008/2/23                          第一章 數位系統與二進位碼                       18
二進制的加法運算
                 電腦內部的主要運算其實只是加
                法運算而己,其餘的減法、乘法、
                除法…等種種運算都只是加法運算
                的應用罷了。
       例題
        l   試計算二進制數01011+10101=?
            (A)100110 (B)100010 (C)100000 (D)101110
       二進制的減法運算
        l   其實在電腦中二進制的減法運算,只不過是
            二進制加法運算的應用罷了。
            l    例如我們可以將A-B改成A+(-B) 來運算,如此即
                 成為加法運算了,但有一點需特別留意的,即
                 A+B的B與A+(-B) 的(-B) 是必須加以區隔的,否
                 則電腦會將其視為是相同的。
            l    因此我們必須將 -B改變成另一種表示方式,即
                 取補數的方式來與A做加法運算。
       人類運算與電腦運算比較:
        人類運算方式:                   電腦運算方式:
         0 – 0= 0                  0 – 0 = 0+(-0) = 0
         0 – 1= -1                 0 – 1 = 0+(-1) = -1
         1 – 0= 1                  1 – 0 = 1+(-0) = 1
         1 – 1= 0                  1 – 1 = 1+(-1) = 0
例題:
1-3 數字基底的轉換
    十進位整數的基底轉換
       l    十進位整數N10部分轉換為以 r 為基底之數字
            => 利用除法
                N         n−1           n−2
             1. r = a n r     + a n −1r     + .... + a 2 r 1
                                                             + a1 = Q1 餘數 a0
                Q
             2. 1 = an r n −2 + an−1r n−3 + .... + a2 = Q2             餘數 a1
                r
             3. Q2 = an r n−3 + an−1r n−4 + .... + a3 = Q3             餘數 a2
                  r
            ……
            直到商數為0為止‚而每次除法後所得的餘數即為以 r 為基底
            數字的係數. Ü N = (a a .....a a a )
                                   10       n n−1    2 1 0 r
                                                     n−1
                                        = an r + an−1r
                                             n
                                                          + .... + a2 r 2 + a1r1 + a0 r 0
2008/2/23                                第一章 數位系統與二進位碼                                      25
Digital Design, 4th Ed
       數字基底的轉換範例
            Example 1:十進數41轉換成二進數
                       整數商    餘數                     係數
            41/2   =    20 +  1/2                   a0 = 1
            20/2   =    10 +   0                    a1 = 0
            10/2   =     5 +   0                    a2 = 0
             5/2  =      2 +  1/2                   a3 = 1
             2/2  =      1 +   0                    a4 = 0
             1/2  =      0 +  1/2                   a5 = 1
       Number-Base Conversions
        l   The arithmetic process can be manipulated more
            conveniently as follows:
       Number-Base Conversions
        l   Example 1.2
            l    Convert decimal 153 to octal. The required base r is 8.
        l   Example1.3
            l    Convert (0.6875)10 to binary.
                 l   The process is continued until the fraction becomes 0 or
                     until the number of digits has sufficient accuracy.
2008/2/23                              第一章 數位系統與二進位碼                            28
       數字基底的轉換(十進數小數部分)
        l   十進位小數部分轉換為以 r 為基底之數字
             利用乘法
             rF = a−1 + a−2 r −1 + a−3r −2 + .... + a−m r −m+1 = a−1 + P1
             rP1 = a−2 + a−3r −1 + a−4 r −2 + .... + a−m r −m+ 2 = a−2 + P2
數字基底的轉換(十進數小數部分)
數字基底的轉換範例
       Number-Base Conversions
        l   Example1.4
            l    Convert (0.513)10 to octal.
1-4 八進位及十六進位數字
         八進位及十六進位數字
      l      二進數轉換成八進數
  (10        110 001 101 011         .   111   100   000   110) 2 = (26153.7406)8
     2        6          1   5   3        7     4     0     6
l 二進數轉換成十六進數
         八進位及十六進位數字
         l   八進數或十六進數轉換成二進數
             EX:
不同數字基底之間的轉換
       1-5 補數(Complement)
            l   補數有兩類型:r-1補數及 r 補數
            l   r-1補數,又稱為diminished radix complement
                對於具有n個位元且基底為r的數字N,則N之 r-1
                的補數定義為 (r n − 1) − N
            EX:
            54670010之9的補數為999999-546700=453299
            01239810之9的補數為999999-012398=987601
            01011012之1的補數為1010010
2008/2/23                     第一章 數位系統與二進位碼            37
Digital Design, 4th Ed
       r-1的補數
            l   r可以表示為任何進制的基底,因此, r-1的補
                數就是基底本身減1的補數。
                l   因為基底是r,所以基底本身減1的補數就稱為r-1的
                    補數。
                l   r代表本身是幾進制的意思,以十進制為例,r就是
                    10,r-1就是9。
例題
       補數
       l     r 的補數,又稱為 radix complement
            對於具有n個位元且基底為 r 的數字N,則N之 r 的補
            數定義為
                 0        , if   N =0        
                 n                           
                r − N     , if   N ≠0        
            è              [(     )     ]
                   r n − N = r n −1 − N +1        (Adding 1 to the r-1’s complement)
       r 補數
        l   r可以表示為任何進制的基底,因此,r的補數就是
            基底本身的補數。
            l    因為基底是r,所以基底本身的補數就稱為r的補數。
例題
       以補數做減法
            l   兩個n位元且基底為 r 的無正負號 M-N 的減法:
                1、將被減數M與減數N之 r 的補數相加。
                  也就是說 M + (r n − N ) = M − N + r n 。
                2、若 M ≥ N,則總和會產生一個末進位r n ,則去掉末進
                  位 剩下的就是 M-N。
                3、若 M < N,則總和不會產生末進位,結果等於r n– (N –
                  M) ,這個數字是 N –M 之 r 的補數。為了得到慣用的
                  答案形式,則將剛才加法所得的和再求其的補數並在前
                  面加上負號,即為答案。
       Example 1.5
        EX:利用10的補數法求 72532 - 3250
                 M= 72532
          N之10的補數= + 96750 (= 105 - 3250)
                和= 169282
         去掉末進位105 = -100000
                答案= 69282
       Example 1.6
        利用10的補數法求 03250 - 72532
               M= 03250
         N之10的補數= + 24768 (= 105 - 72532)
               和= 30718
         沒有末進位產生,所以要對計算結果執行補數運
         算,因此,答案為-(30718之10的補數)
                = -69282
        è我們可以用進位來判斷是否要對總和作補數運算
       Example 1.7
        l   Given the two binary numbers X = 1010100 and Y =
            1000011, perform the subtraction (a) X – Y and (b) Y − X
            by using 2's complement.
       Example 1.8
      l     Subtraction of unsigned numbers can also be done by means of the (r −
            1)'s complement. Remember that the (r − 1) 's complement is one less
            then the r's complement.
            l   Repeat Example 1.7, but this time using 1's complement.
       1-6 二進位有符號的數字表示法
        l   To represent negative integers, we need a
            notation for negative values.
        l   It is customary to represent the sign with a bit
            placed in the leftmost position of the number.
        l   The convention is to make the sign bit 0 for
            positive and 1 for negative
        l   Example:
       Example
        l   有符號數字
l 無符號數字
       符號大小表示法
            l   符號大小表示法是以二進制的形式來表示的,
                因此凡是非二進制的數,都必須先將其轉換成
                二進制後,才能用符號大小表示法來表示。
            l   所謂的符號大小表示法,即是將所有的表示位
                元之最左邊的一個位元規定為符號位元,若於
                符號位元內填上“0”代表正數,填上“1”代表負
                數。
            l   對於符號大小表示法而言,正數與負數的表示
                方法只在於符號位元的不同而已,其數值位元
                是沒有任何差異的。
2008/2/23                第一章 數位系統與二進位碼                     51
Digital Design, 4th Ed
-1
                             -2
                                          Two
-3
-4
       Arithmetic Addition
        l   The addition of two numbers in the signed-
            magnitude system follows the rules of ordinary
            arithmetic. If the signs are the same, we add the
            two magnitudes and give the sum the common
            sign. If the signs are different, we subtract the
            smaller magnitude from the larger and give the
            difference the sign if the larger magnitude.
       Arithmetic Subtraction
        l       In 2’s-complement form:
            l      Take the 2’s complement of the subtrahend (including
                   the sign bit) and add it to the minuend (including sign
                   bit).
            l      A carry out of sign-bit position is discarded.
                                   (± A) − (+ B ) = ( ± A) + (− B )
                                   (± A) − (− B ) = (± A) + ( + B)
                Example:
                                               00000111 (+ 7)
2008/2/23                                 第一章 數位系統與二進位碼                      55
Digital Design, 4th Ed
       1-7 常見之二進數碼
        l       BCD碼
        l       葛雷碼
        l       超3碼
        l       2421加權碼
        l       84-2-1加權碼
        l       ASCII字元碼
       二進制碼
            l   所謂二進制碼即是由一長串的
                二進制元素0與1所組合而成,
                例如1011001111100101(2)即
                是二進制碼,任何二進制碼的
                數所代表的意義,即是指其所
                代表十進制的數為何?
            l   二進制碼上的每一個位元,均
                有其固定的加權值,由右至左
                每一個位元均有著兩倍的加權
                值。
例題:
BCD碼
            l    常久以來人類慣用十進制來做算術運算,但新
                 科技的電腦卻只能使用二進制來做算術運算,
                 因此為了讓電腦能夠協助人類做一些算術運
                 算,所以使用了BCD碼來作為人類與電腦之間
                 的一種溝通橋樑。
            l    所謂BCD碼就是固定以四個位元(4 bit)的二進制
                 來表示十進制的一種數碼,因此BCD碼稱為“二
                 進制十進碼”。
8421(BCD)碼與十進制的對照關係表
Table 1-4
例題:
BCD加法
       Binary Codes
        Example:
            l    Consider the addition of 184 + 576 = 760 in BCD
l Decimal Arithmetic
       84-2-1碼
            l   由前面可以知道8421碼字面上的數字 8421是代
                表四個加權值的意思。同樣地,84-2-1碼字面
                上的數字 “84-2-1”必也是加權值。
            l   兩者之間的差異只在於後面兩個加權值不同而
                已,亦即一個為正值,而另一個為負值。由此
                可知,84-2-1碼也與8421碼一樣均屬於四個位
                元的二進制碼的特殊應用。
84-2-1碼與十進制的對照關係表
例題:
2421碼
            l    由前面可以知道8421碼字面上的數字 8421是代
                 表四個加權值的意思。同樣地,2421碼字面上
                 的數字 “2421”必也是加權值。
            l    兩者之間的差異只在於最左邊的一個加權值不
                 同而已,亦即一個為8,而另一個為2。
2421碼與十進制的對照關係表
例題:
加三碼(Excess-3 code)
            l    加三碼必須是由BCD碼轉換而來,舉凡一切非
                 BCD碼者,均必須先行轉換成BCD碼後才能正
                 確轉換成加三碼。
            l    我們只要將BCD碼加上0011即成加三碼。
加三碼與BCD碼、十進制的對照表
例題:
            l   格雷碼是非加權碼的一種,因為它的每一個位
                元均無固定的乘冪或加權值,因此無法拿來作
                為算術運算之用。
            l   格雷碼是由二進制碼轉換而來,因此欲轉換成
                格雷碼時,必須先行轉換為二進制碼後才能轉
                換成格雷碼。
            l   格雷碼的優點是相鄰編碼之間只差一個位元,
                其餘一樣。
格雷碼與二進制碼的對照關係表
例題:
       標準BCD碼
            l    標準BCD碼的英文全名為BCD Interchange
                 Code,所以又稱為BCDIC碼或BCD交換碼。
            l    標準BCD碼是BCD碼的擴展,比BCD碼多兩個
                 位元,以六個位元組成,即在BCD碼的前面加
                 兩個位元作為區域位元(Zone Bits),BCD碼
                 原來的四個位元則作為數字位元(Digital
                 Bits)。
標準BCD碼格式
標準BCD碼字符表
例題:
ASCII碼
            l    ASCII碼的英文全名是American Standard
                 Code for Information Interchange,中文稱為
                 美國標準資訊交換碼。
            l    在當時美國國家標準局(American National
                 Standard Institute,簡稱ANSI)為了要讓各家
                 廠商所製造的電腦能有一致的文數字碼可以相
                 通,不會因為電腦廠牌不同而無法相互溝通,
                 因此制定了一套標準化的資訊交換碼,使得不
                 同的電腦都有共同的標準可依循。
2008/2/23                    第一章 數位系統與二進位碼               81
Digital Design, 4th Ed
ASCII碼格式
ASCII碼格式
        ASCII碼格式
    l       American Standard Code for Information
            Interchange (Refer to Table 1.7)
    l       A popular code used to represent information sent
            as character-based data.
    l       It uses 7-bits to represent:
            l    94 Graphic printing characters.
            l    34 Non-printing characters
    l       Some non-printing characters are used for text
            format (e.g. BS = Backspace, CR = carriage return)
    l       Other non-printing characters are used for record
            marking and flow control (e.g. STX and ETX start
            and end text areas).
2008/2/23                               第一章 數位系統與二進位碼                              84
ASCII Properties
ASCII碼格式
ASCII碼的字符表
例題:
EBCDIC碼
            l    EBCDIC碼的英文全名是Extended Binary
                 Coded Decimal Interchange Code,稱為擴展
                 的BCDIC碼,或稱擴展的BCD交換碼。
            l    EBCDIC碼是BCDIC碼的擴展,比BCDIC碼多
                 兩個位元,比BCD碼多四個位元,以八個位元
                 組成,即在BCD碼的前面加四個位元作為區域
                 位元(Zone Bits),BCD碼原來的四個位元則
                 作為數字位元(Digital Bits)。
EBCDIC碼的格式
EBCDIC碼的字符表
例題:
       Error-Detecting Code
       l    同位元碼(Parity code)不但可以用來檢查數字,也
            可以用來檢查資料,是屬於資料檢查碼的一種。
       l    其作法是在資料位元的前面或後面,加入一個位
            元的0或1,使其成為偶數個1或奇數個1。
       l    Example
            l   Consider the following two characters and their even
                and odd parity:
       Error-Detecting Code
        l   Redundancy (e.g. extra information), in the form
            of extra bits, can be incorporated into binary code
            words to detect and correct errors.
        l   A simple form of redundancy is parity, an extra bit
            appended onto the code word to make the
            number of 1’s odd or even. Parity can detect all
            single-bit errors and some multiple-bit errors.
        l   A code word has even parity if the number of 1’s
            in the code word is even.
        l   A code word has odd parity if the number of 1’s in
            the code word is odd.
2008/2/23                     第一章 數位系統與二進位碼                       94
同位元碼與3 bits的二進制碼對照表
                                範例
                                l   The other major
                                    component of a digital
                                    system
                                    l   circuit elements to
                                        manipulate individual bits of
                                        information
       1-9 二進位邏輯
            l   三個基本的邏輯運算
                AND 、 OR 、NOT 。
            1、AND: x ⋅ y = z ,z=1 充要條件是x=1且y=1;
                   否則z=0。
            2、OR: x + y = z ,若 x=1或y=1或同時 x=1且y=1
                   則 z=1;如果同時 x=0且y=0 則 z=0。
            3、NOT: x′ = z 或 x = z ,如果 x=0 則 z=1;
                   如果 x=1 則 z=0。
2008/2/23                              第一章 數位系統與二進位碼            100
       二進位邏輯真值表
        l       真值表
                          AND                OR        NOT
                         x y    x⋅ y     x    y x+ y   x   x′
                         0 0    0       0    0     0   0   1
                         0 1    0       0    1     1   1   0
                         1 0    0       1    0     1
                         1 1    1       1    1     1
       二進位邏輯
        l   Example of binary
            signals
                         Time
                                                Continuous in
                                                value & time
                  Analog
                   Digital                       Discrete in
                                                  value &
            Asynchronous                        continuous in
                                                     time
              Synchronous                        Discrete in
                                                value & time
                         OUTPUT                   INPUT
                                    5.0
            HIGH                                          HIGH
                                    4.0
                                    3.0
                                                            Threshold
                                    2.0                     Region
                                    1.0
            LOW                                           LOW
                                    0.0
                                   Volts
2008/2/23                         第一章 數位系統與二進位碼                         104
       邏輯閘
        l   Graphic Symbols and Input-Output Signals
            for Logic gates:
       Multiple Inputs
        l   Graphic Symbols and Input-Output Signals
            for Logic gates
                         Question?
        l   Exercises
l 繳交日期: