1 / 24

BCC

BCC. 數字系統. r 進位:轉成 10 進位. r 代表「基底」 10 進位: r = 10 2 進位: r = 2 D r : 表示某個 r 進位的數 (r 不寫,表 r=10) D r ≡ (d n-1 d n-2 …d 0 ‧d -1 d -2 …d -q ) r 135.246 10 = (1*10 2 +3*10 1 +5*10 0 +2*10 -1 +4*10 -2 +6*10 -3 ) 10 147.256 8 = (1*8 2 +4*8 1 +7*8 0 +2*8 -1 +5*8 -2 +6*8 -3 ) 10

Télécharger la présentation

BCC

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. BCC 數字系統 聯合大學資工系 周念湘 nschou@nuu.edu.tw

  2. r進位:轉成10進位 • r代表「基底」 • 10進位:r=10 • 2進位:r= 2 • Dr:表示某個r進位的數(r不寫,表r=10) • Dr ≡ (dn-1dn-2…d0‧d-1d-2…d-q)r • 135.24610= (1*102+3*101+5*100+2*10-1+4*10-2+6*10-3)10 • 147.2568= (1*82+4*81+7*80+2*8-1+5*8-2+6*8-3)10 • 就某一個「數量」而言,不同進位,只是表示法不同,他們都是表示「同一個數量」,理論上必須相同。 • 整數:各進位間,有「1對1」對應 • 實數:10進位轉成2進位時,可能無法用有限的位數表示! • 在電腦的世界中, 整數、實數以不同方式存放 聯合大學資工系 周念湘 nschou@nuu.edu.tw

  3. 10 進位:轉成r進位 • 「整數、小數」分開算 • 整數:連除r,取餘數 • 小數:連乘r,取整數 • 【範例】766.2812510=( ? )8 • 766.2812510=(1376.22)8 聯合大學資工系 周念湘 nschou@nuu.edu.tw

  4. 整數:n位數, r補數 • r代表「基底」 • 10進位:r=10 • 2進位:r= 2 • Dr:表示某個r進位的整數 • 我們必須明訂這個整數是幾位數(令為n), 位數不夠需補0 • Dr ≡ (dn-1dn-2…d0)r • r補數,代表r進位系統中的「負數」 • 有了r補數,我們就可用加法來做減法,簡化硬體線路 • ∵A-B = A+(-B) = A+(B的r補數) • ∴-B= B的r補數 聯合大學資工系 周念湘 nschou@nuu.edu.tw

  5. 整數:n位數, r補數 • 既然,「r補數」代表r進位系統中的「負數」 • 『Dr ≡ (dn-1dn-2…d0)r>0』的r補數=-Dr =rn -Dr • ∵ Dr + -Dr =0 而 Dr +(rn -Dr) = rn = (1n0n-10n-2…00)r • ∵我們只計n位數 ∴將1n丟掉(忽略)即得到正確的答案0 • 例子:求(1234)10的10補數: 求(1011)2的2補數: 10000 10000 1234 (- 1011 (- ----------- ------------- 8766 = (104-123410) 0101 = (24-10112) • 1234 (+ 1011 (+ ----------- -------------10000 = (10410) 10000 = (24) 聯合大學資工系 周念湘 nschou@nuu.edu.tw

  6. 整數:n位數, r補數 • 「r補數」比較不好算  「r-1補數」+1, 令r’=r-1 • 『 Dr ≡ (dn-1dn-2…d0)r 』的r-1補數=-Dr -1=rn -1-Dr= (r’n-1r’n-2…r’0)r- (dn-1dn-2…d0)r • 例子:求(1234)10的9補數: 求(1011)2的1補數: 9999 1111 1234 (- 1011 (- ----------- ------------- 8765 = (104-1-123410) 0100 = (24-1-10112) • 「r補數」=「r-1補數」+1 • 例子:求(1234)10的10補數: 求(1011)2的2補數: 8765 = (104-1-123410) 0100 = (24-1-10112) 1 (+ 1 (+ ------------- ------------- 8766 = (104-123410) 0101 = (24-10112) 剛好:10, 01 聯合大學資工系 周念湘 nschou@nuu.edu.tw

  7. 二進位 • 電腦為何使用「二進位」? • 因為電腦的「元件」都是「雙態(2種狀態)」元件 • 二極體:P極、N極 • 磁蕊 :南極、北極 • 電壓 :高電壓、低電壓 • 這些「雙態」元件的狀態都可利用「電」改變之 • 其中一種狀態稱之為「0」,另一種狀態稱之為「1」 • 若該元件可持續維持在某一狀態,直至我們改變它為止,則我們稱之為「記憶體元件」 • 1個這樣的記憶體元件,我們稱其為「一個bit」 聯合大學資工系 周念湘 nschou@nuu.edu.tw

  8. 二進位 • 為何「元件」在任一時間點,只能存1或0? • i.e. 為何不能同時存1和0? • 以「磁蕊」為例說明之: • 假設南極為0,北極為1 • 同一個磁蕊同一時間只能有一種極性, 因此, 只能有一種狀態, 要麼是0, 要麼是1 • 因此,當有人說「把0存入某記憶體(元件)中」:意指「將該記憶體(元件)的狀態改為0」 聯合大學資工系 周念湘 nschou@nuu.edu.tw

  9. bit | nibble | Byte • 任何資料在電腦中,必是以「0101…」的形式存入,該形式稱之為「bit pattern」 吾人以「β2」表示之。 • 10進位無號整數(α10)轉成2進位(β2)寫成「α10=β2」 • 1bit只能表示2種狀態 • 當我們需要多種狀態時,就需要多個bit組合起來,看成一組。 • nibble: 4 bits看成一組  有 24= 16種狀態 (bit pattern) • Byte : 8 bits看成一組  有 28=256種狀態 (bit pattern) • 4bit:雖然有16種狀態(bit pattern), 但是,任一時間點,只能「存」其中一種狀態(bit pattern) 聯合大學資工系 周念湘 nschou@nuu.edu.tw

  10. 有號整數存入電腦的方式:n-bits • 2補數(2’s complement):儲存負整數 • 1補數(1’s complement):為了方便計算2補數 • Excess 2n-1:儲存實數的指數 聯合大學資工系 周念湘 nschou@nuu.edu.tw

  11. 1’s complement:1補數 有號整數 • (1’s complement): n-bits • D10β2= α10 • formula: • D10 ≧0  D10=B2≡β2 • D10<0  (1) -D10 =P2≧0(2) β2≡「P2的1補數」=~P2=(2n-1)10-P2 =(2n-1)10+D10 = α10 • ~P2 ≡{P2各bit:01, 10} • 兩種0:+0, -0 • 以4-bits為例:0000, 1111 1補數 聯合大學資工系 周念湘 nschou@nuu.edu.tw

  12. 2’s complement:2補數 • (1’s complement)+1 • D10β2= α10 • formula: • D10≧0D10=B2=β2 MSB=0 • D10<0  //D10=-410(1) -D10 = P2 //P2=410=01002(2) β2= P2的2補數(11002)=~P2+1=(2n-1)10-P2+1=(2n)10+D10 = α10MSB=1 • X-Y=X+(-Y) -Y=Y的2補數 2補數 12 聯合大學資工系 周念湘 nschou@nuu.edu.tw

  13. 2’s complement:2補數 • D10<0 「令S’2=D’10 」 • D10β2≡(1S’)2=α10=2n+D10 • =2n-1+ S’2=2n-1+D’10 •  D10 =2n-1+ D’10 -2n = D’10 -2n-1 • 舉例: • D10=-6  10102=1010=23+0102 • S’2=0102=210=D’10 • D10=-6=210-23=D’10-23=D’10-24-1 聯合大學資工系 周念湘 nschou@nuu.edu.tw

  14. (A+B)2: illegal : cn⊕cn-1=1 • C=cncn-1cn-2cn-3…c0A= an-1an-2an-3…a0B= bn-1bn-2bn-3…b0 • -2n-1≦ A+B=Z < 2n-1 • (cn=0)⊕(cn-1=1)=1 overflow • cn=0, cn-1=1  an-1=bn-1=0 • C=01cn-2cn-3…c0A= 0an-2an-3…a0 >0B= 0bn-2bn-3…b0 >0 (+-------------------------------Z= 1zn-2zn-3…z0 <0 (正+正變負) • cn-1=1  A+B≧2n-1 →← overflow 聯合大學資工系 周念湘 nschou@nuu.edu.tw

  15. (A+B)2: illegal : cn⊕cn-1=1 • C=cncn-1cn-2cn-3…c0A= an-1an-2an-3…a0B= bn-1bn-2bn-3…b0 • -2n-1≦ A+B=Z < 2n-1 • (cn=1)⊕(cn-1=0)=1  underflow • cn=1, cn-1=0  an-1=bn-1=1 (A,B<0) • C=10cn-2cn-3…c0A= 1an-2an-3…a0 <0 令A=1A’ B= 1bn-2bn-3…b0 <0 (+ 令 B=1B’ -------------------------------Z= 0zn-2zn-3…z0 ≧0 (負+負變正) • cn-1=0  0≦A’+B’<2n-1 (Z>0)A+B=(A’- 2n-1)+(B’- 2n-1) =A’+B’- 2n <2n-1-2n <-2n-1 →← underflow 聯合大學資工系 周念湘 nschou@nuu.edu.tw

  16. (A+B)2: legal : cn⊕cn-1=0 • C=cncn-1cn-2cn-3…c0A= an-1an-2an-3…a0B= bn-1bn-2bn-3…b0 • -2n-1≦ A+B < 2n-1 • cn⊕cn-1=0 • cn=1, cn-1=1  drop cn • 試證明 A*B<0 則 A+B>0 • cn=0, cn-1=0  do nothing • 試證明 A*B<0 則 A+B<0 • A+B<0 (MSB=1) • 轉成10進位時,結果需再取2補數,前面加上負號。 聯合大學資工系 周念湘 nschou@nuu.edu.tw

  17. (A*B)2:「left shift」「+」 聯合大學資工系 周念湘 nschou@nuu.edu.tw

  18. (A/B)2: 「left shift」「-」 • 110011002/1012R1 = 110011002 -(101)2*25Q1=1*25 = 1011002R2 = 1011002 -(101)2*23 Q2=1*23= 1002Ans: Q=Q1+Q2=1000002+10002=1010002 =4010 R=R2 = 1002 = 410 • 驗證:(11001100)2 /1012=20410/510=40餘4 • 減法會用「加2補數」做 聯合大學資工系 周念湘 nschou@nuu.edu.tw

  19. Excess 2n-1表示法 2’s complement Excess-8 • excess-8 (23) • D10β2= α10 = (D+2n-1) 10 • D10=6β2=11102 =1410=(6+8)10 • D10≧0β2是2n-1 -D10的2補數 • D10<0β2是2n-1+D10的2進位 • β2= α10  α10 - 2n-1β2= α10  α10 if α10 <2n-1α10-2n otherwise 聯合大學資工系 周念湘 nschou@nuu.edu.tw

  20. 實數(Floating Point)儲存方式 • 以 8bits 舉例說明: 聯合大學資工系 周念湘 nschou@nuu.edu.tw

  21. 31 0 31 Sign • BiasedExponent 23excess-bias 22 significand 0Mantissa IEEE 754:1. single precision +127 value=(-1)S*(1+M)*2(BE-Bias) (Bias=127) 聯合大學資工系 周念湘 nschou@nuu.edu.tw

  22. 31 0 31 Sign • BiasedExponent 23excess-127 22 significand 0Mantissa IEEE 754:1. single precision • value=(-1)S*(1+M)*2(BE-Bias) (Bias=127) • -3.1415= (-1)1*(11.00100100001…)= (-1)1*(1.100100100001…) *21 = (-1)1*(1.100100100001…) *2(128-127) 10000000 聯合大學資工系 周念湘 nschou@nuu.edu.tw

  23. 64 0 63 Sign • BiasedExponent 52excess-1023 51 significand 0Mantissa IEEE 754:2. double precision +1023 value=(-1)S*(1+M)*2(BE-Bias) (Bias=1023) 聯合大學資工系 周念湘 nschou@nuu.edu.tw

  24. 64 0 63 Sign • BiasedExponent 52excess-1023 51 significand 0Mantissa IEEE 754:2. double precision • value=(-1)S*(1+M)*2(BE-Bias) (Bias=1023) • -3.1415= (-1)1*(11.00100100001…)= (-1)1*(1.100100100001…) *21 = (-1)1*(1.100100100001…) *2(1024-1023) 10000000000 聯合大學資工系 周念湘 nschou@nuu.edu.tw

More Related