問題 5.9 綜合了計算機組成原理、數字邏輯和離散數學中的關鍵概念,旨在幫助學生理解二進制算術運算的硬件實現、邏輯門與算術運算的關系,以及如何使用數學方法來驗證數字系統的正確性。它強調了從規范到實現再到驗證的完整過程。
思想
- 函數抽象: num ? ( α n ) \operatorname{num}(\alpha_{n}) num(αn?):將二進制字符串映射到非負整數
- 遞歸: num ? ( α n + 1 ) = a n + 1 2 n + 1 + num ? ( α n ) \operatorname{num}(\alpha_{n + 1})=a_{n + 1}2^{n + 1}+\operatorname{num}(\alpha_{n}) num(αn+1?)=an+1?2n+1+num(αn?)
- 用求和位和進位來表示兩個二進制數的加法:
思路
- 加法器的抽象規范–>加法器的電路實現–>數學方法驗證實現是否符合規范
拓展問題
- 問題5.9都考察了哪些知識點?講到了哪些重要的結論?
- 如何將問題5.9的結論推廣到十進制?
Problem 5.9
For any binary string α \alpha α, let num ? ( α ) \operatorname{num}(\alpha) num(α) be the nonnegative integer it represents in binary notation (possibly with leading zeroes). For example, num ? ( 10 ) = 2 \operatorname{num}(10)=2 num(10)=2, and num ? ( 0101 ) = 5 \operatorname{num}(0101)=5 num(0101)=5.
An n + 1 n + 1 n+1-bit adder adds two n + 1 n + 1 n+1-bit binary numbers. More precisely, an n + 1 n + 1 n+1-bit adder takes two length n + 1 n + 1 n+1 binary strings
α n : : = a n . . . a 1 a 0 , β n : : = b n . . . b 1 b 0 , \begin{align*} \alpha_{n}&::=a_{n}...a_{1}a_{0},\\ \beta_{n}&::=b_{n}...b_{1}b_{0}, \end{align*} αn?βn??::=an?...a1?a0?,::=bn?...b1?b0?,?
and a binary digit C 0 C_{0} C0? as inputs, and produces a length- ( n + 1 ) (n + 1) (n+1) binary string
σ n : : = s n . . . s 1 s 0 , \sigma_{n}::=s_{n}...s_{1}s_{0}, σn?::=sn?...s1?s0?,
and a binary digit c n + 1 c_{n + 1} cn+1? as outputs, and satisfies the specification:
num ? ( α n ) + num ? ( β n ) + c 0 = 2 n + 1 c n + 1 + num ? ( σ n ) . (5.9) \operatorname{num}(\alpha_{n})+\operatorname{num}(\beta_{n})+c_{0}=2^{n + 1}c_{n + 1}+\operatorname{num}(\sigma_{n}). \tag{5.9} num(αn?)+num(βn?)+c0?=2n+1cn+1?+num(σn?).(5.9)
There is a straightforward way to implement an n + 1 n + 1 n+1-bit adder as a digital circuit: an n + 1 n + 1 n+1-bit ripple-carry circuit
has 1 + 2 ( n + 1 ) 1 + 2(n + 1) 1+2(n+1) binary inputs
a n , . . . , a 1 , a 0 , b n , . . . , b 1 , b 0 , c 0 , a_{n},...,a_{1},a_{0},b_{n},...,b_{1},b_{0},c_{0}, an?,...,a1?,a0?,bn?,...,b1?,b0?,c0?,
and n + 2 n + 2 n+2 binary outputs,
c n + 1 , s n , . . . , s 1 , s 0 . c_{n + 1},s_{n},...,s_{1},s_{0}. cn+1?,sn?,...,s1?,s0?.
As in Problem 3.6, the ripple-carry circuit is specified by the following formulas:
s i : : = a i ⊕ b i ⊕ c i c i + 1 : : = ( a i ∧ b i ) ∨ ( a i ∧ c i ) ∨ ( b i ∧ c i ) \begin{align*} s_{i}&::=a_{i}\oplus b_{i}\oplus c_{i} \tag{5.10}\\ c_{i + 1}&::=(a_{i} \land b_{i}) \lor (a_{i} \land c_{i}) \lor (b_{i} \land c_{i}) \tag{5.11} \end{align*} si?ci+1??::=ai?⊕bi?⊕ci?::=(ai?∧bi?)∨(ai?∧ci?)∨(bi?∧ci?)?(5.10)(5.11)?
for 0 ≤ i ≤ n 0\leq i\leq n 0≤i≤n, where we follow the convention that 1 1 1 corresponds to T and 0 0 0 corresponds to F.
(a) Verify that definitions (5.10) and (5.11) imply that
a n + b n + c n = 2 c n + 1 + s n . (5.12) a_{n}+b_{n}+c_{n}=2c_{n + 1}+s_{n}. \tag{5.12} an?+bn?+cn?=2cn+1?+sn?.(5.12)
for all n ∈ N n \in \mathbb{N} n∈N.
證明:對任意的 n ∈ N n\in\mathbb{N} n∈N,
- 左邊 = a n + b n + c n \text{左邊}=a_n+b_n+c_n 左邊=an?+bn?+cn?。
- 可用 1 1 1 位的行波進位加法器來實現。它的輸入為 a n a_n an?, b n b_n bn?, c n c_n cn?,輸出為 c n + 1 c_{n+1} cn+1?, s n s_n sn?。
- 根據 (5.10) 和 (5.11) 有, s n = a n ⊕ b n ⊕ c n s_{n}=a_{n}\oplus b_{n}\oplus c_{n} sn?=an?⊕bn?⊕cn?, c n + 1 = ( a n ∧ b n ) ∨ ( b n ∧ c n ) ∨ ( c n ∧ a n ) c_{n+1}=(a_{n}\land b_{n})\lor(b_{n}\land c_{n})\lor(c_{n}\land a_{n}) cn+1?=(an?∧bn?)∨(bn?∧cn?)∨(cn?∧an?)。
- 右邊 = 2 c n + 1 + s n = 2 [ ( a n ∧ b n ) ∨ ( b n ∧ c n ) ∨ ( c n ∧ a n ) ] + a n ⊕ b n ⊕ c n \text{右邊}=2c_{n+1}+s_n=2[(a_{n}\land b_{n})\lor(b_{n}\land c_{n})\lor(c_{n}\land a_{n})]+a_{n}\oplus b_{n}\oplus c_{n} 右邊=2cn+1?+sn?=2[(an?∧bn?)∨(bn?∧cn?)∨(cn?∧an?)]+an?⊕bn?⊕cn?。
下面使用真值表來驗證。
a n a_{n} an? | b n b_{n} bn? | c n c_{n} cn? | a n + b n + c n a_{n} + b_{n} + c_{n} an?+bn?+cn? | s n = a n ⊕ b n ⊕ c n s_{n}=a_{n}\oplus b_{n}\oplus c_{n} sn?=an?⊕bn?⊕cn? | a n ∧ b n a_{n}\land b_{n} an?∧bn? | b n ∧ c n b_{n}\land c_{n} bn?∧cn? | c n ∧ a n c_{n}\land a_{n} cn?∧an? | c n + 1 = ( a n ∧ b n ) ∨ ( b n ∧ c n ) ∨ ( c n ∧ a n ) c_{n+1} = (a_{n} \land b_{n}) \lor (b_{n}\land c_{n})\lor (c_{n}\land a_{n}) cn+1?=(an?∧bn?)∨(bn?∧cn?)∨(cn?∧an?) | 2 c n + 1 + s n 2c_{n+1} + s_{n} 2cn+1?+sn? |
---|---|---|---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
1 | 0 | 0 | 1 | 1 | 0 | 0 | 0 | 0 | 1 |
0 | 1 | 0 | 1 | 1 | 0 | 0 | 0 | 0 | 1 |
0 | 0 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 1 |
1 | 1 | 0 | 2 | 0 | 1 | 0 | 0 | 1 | 2 |
1 | 0 | 1 | 2 | 0 | 0 | 0 | 1 | 1 | 2 |
0 | 1 | 1 | 2 | 0 | 0 | 1 | 0 | 1 | 2 |
1 | 1 | 1 | 3 | 1 | 1 | 1 | 1 | 1 | 3 |
(b) Prove by induction on n n n that an n + 1 n + 1 n+1-bit ripple-carry circuit really is an n + 1 n + 1 n+1-bit adder, that is, its outputs satisfy (5.9).
Hint: You may assume that, by definition of binary representation of integers,
num ? ( α n + 1 ) = a n + 1 2 n + 1 + num ? ( α n ) . (5.13) \operatorname{num}(\alpha_{n + 1})=a_{n + 1}2^{n + 1}+\operatorname{num}(\alpha_{n}). \tag{5.13} num(αn+1?)=an+1?2n+1+num(αn?).(5.13)
證明:使用強歸納法。
歸納假設 P ( n ) P(n) P(n) 為公式 (5.9)。
基礎情形: n = 0 n=0 n=0
- 左邊 = num ? ( α 0 ) + num ? ( β 0 ) + c 0 \text{左邊}=\operatorname{num}(\alpha_{0})+\operatorname{num}(\beta_{0})+c_{0} 左邊=num(α0?)+num(β0?)+c0?。
- 右邊 = 2 c 1 + num ? ( σ 0 ) \text{右邊}=2c_1+\operatorname{num}(\sigma_{0}) 右邊=2c1?+num(σ0?)。
- 根據 n + 1 n+1 n+1位加法器的規范可知: α 0 = a 0 \alpha_0=a_0 α0?=a0?, β 0 = b 0 \beta_0=b_0 β0?=b0?, σ 0 = s 0 \sigma_0=s_0 σ0?=s0?。因為只有 1 1 1 位,所以 num ? ( α 0 ) = a 0 \operatorname{num}(\alpha_{0})=a_0 num(α0?)=a0?, num ? ( β 0 ) = b 0 \operatorname{num}(\beta_{0})=b_0 num(β0?)=b0?, num ? ( σ 0 ) = s 0 \operatorname{num}(\sigma_{0})=s_0 num(σ0?)=s0?。
- 左邊 = num ? ( α 0 ) + num ? ( β 0 ) + c 0 = a 0 + b 0 + c 0 \text{左邊}=\operatorname{num}(\alpha_{0})+\operatorname{num}(\beta_{0})+c_{0}=a_0+b_0+c_0 左邊=num(α0?)+num(β0?)+c0?=a0?+b0?+c0?。
- 右邊 = 2 c 1 + num ? ( σ 0 ) = 2 c 1 + s 0 \text{右邊}=2c_1+\operatorname{num}(\sigma_{0})=2c_1+s_0 右邊=2c1?+num(σ0?)=2c1?+s0?。
- 根據公式(5.12)可知, 左邊 = 右邊 \text{左邊}=\text{右邊} 左邊=右邊。
歸納步驟:
- 假設 P ( k ) P(k) P(k) 對所有 k ≤ n k\leq n k≤n 都成立。
- 當 k = n + 1 k=n+1 k=n+1 時, 左邊 = num ? ( α n + 1 ) + num ? ( β n + 1 ) + c 0 \text{左邊}=\operatorname{num}(\alpha_{n+1})+\operatorname{num}(\beta_{n+1})+c_{0} 左邊=num(αn+1?)+num(βn+1?)+c0?。
- 根據公式(5.13)有:
左邊 = a n + 1 2 n + 1 + num ? ( α n ) + b n + 1 2 n + 1 + num ? ( β n ) + c 0 = ( a n + 1 2 n + 1 + b n + 1 2 n + 1 ) + ( num ? ( α n ) + num ? ( β n ) + c 0 ) = 2 n + 1 ( 2 c n + 2 + s n + 1 ? c n + 1 ) + ( 2 n + 1 c n + 1 + num ? ( σ n ) ) = 2 n + 2 c n + 2 + 2 n + 1 s n + 1 ? 2 n + 1 c n + 1 + 2 n + 1 c n + 1 + s n = 2 n + 2 c n + 2 + ( 2 n + 1 s n + 1 + num ? ( σ n ) ) = 2 n + 2 c n + 2 + num ? ( σ n + 1 ) = 右邊 . \begin{align*} \text{左邊}&=a_{n+1}2^{n+1}+\operatorname{num}(\alpha_{n})+b_{n+1}2^{n+1}+\operatorname{num}(\beta_{n})+c_0\\ &=(a_{n+1}2^{n+1}+b_{n+1}2^{n+1})+(\operatorname{num}(\alpha_{n})+\operatorname{num}(\beta_{n})+c_0)\\ &=2^{n+1}(2c_{n+2}+s_{n+1}-c_{n+1})+(2^{n+1}c_{n+1}+\operatorname{num}(\sigma_{n}))\\ &=2^{n+2}c_{n+2}+2^{n+1}s_{n+1}-2^{n+1}c_{n+1}+2^{n+1}c_{n+1}+s_n\\ &=2^{n+2}c_{n+2}+(2^{n+1}s_{n+1}+\operatorname{num}(\sigma_{n}))\\ &=2^{n+2}c_{n+2}+\operatorname{num}(\sigma_{n+1})\\ &=\text{右邊}. \end{align*} 左邊?=an+1?2n+1+num(αn?)+bn+1?2n+1+num(βn?)+c0?=(an+1?2n+1+bn+1?2n+1)+(num(αn?)+num(βn?)+c0?)=2n+1(2cn+2?+sn+1??cn+1?)+(2n+1cn+1?+num(σn?))=2n+2cn+2?+2n+1sn+1??2n+1cn+1?+2n+1cn+1?+sn?=2n+2cn+2?+(2n+1sn+1?+num(σn?))=2n+2cn+2?+num(σn+1?)=右邊.?
綜上所述, P ( n ) P(n) P(n) 對任意的 n ∈ N n\in\mathbb{N} n∈N 都成立。