Presentation is loading. Please wait.

Presentation is loading. Please wait.

12008Fall Semester Introduction to Computer Science Chapter 2 Data Representation and Storage Speaker: Ying Chih Lin Department of Applied Mathematics.

Similar presentations


Presentation on theme: "12008Fall Semester Introduction to Computer Science Chapter 2 Data Representation and Storage Speaker: Ying Chih Lin Department of Applied Mathematics."— Presentation transcript:

1 12008Fall Semester Introduction to Computer Science Chapter 2 Data Representation and Storage Speaker: Ying Chih Lin Department of Applied Mathematics National Sun Yat-sen University

2 Introduction to Computer Science 22008Fall Semester Outline Data Storage 4. Floating Point 3. Numeric Representation 2. Number Systems 1.

3 Introduction to Computer Science 32008Fall Semester Data Types data 1,2,3… 01110…

4 Introduction to Computer Science 42008Fall Semester Data Manipulation  電腦內儲存的資料只有 0 和 1 ,然而我們所使用的 資料型態卻有許多種。 Word, 記事本, … ACDsee, 小畫家 PhotoImpact, PhotoShop, …

5 Introduction to Computer Science 52008Fall Semester Bit ( 位元 ) & Byte ( 位元組 )  位元 (binary digit ,簡稱 bit) 是數位資訊的基本粒子, 也是電腦儲存或傳遞資料的最小單位,常用 0 或 1 來表示。  當初電腦會採用位元表示資料,主要是因為電子 元件的穩定狀態有兩種:一種是 “ 開 ”( 通常用 1 表示 ) 及一種是 “ 關 ” ( 通常用 0 來表示 ) 。 0 1 1 1 0 1 0 1 0 1 1 … bit 8 bits =1 byte 1 KB = 2 10 bytes 1 MB = 2 10 KB = 2 20 bytes 1 GB = 2 10 MB = 2 30 bytes 1 TB = 2 10 GB = 2 40 bytes 1 PB = 2 10 TB = 2 50 bytes

6 Introduction to Computer Science 62008Fall Semester Number Systems  電腦內使用二進制,我們日常生活中常用的則是 十進制,其他常見的還有八進制和十六進制。  正如同全世界有許多不同的語言在使用,當這些 人相遇時就必須要有一個共同瞭解的語言才能溝 通。

7 Introduction to Computer Science 72008Fall Semester Decimal System  十進位數字系統使用 0,1,2,…,9 共十個數值符號, 由這些符號組成各種數字,而每個數字中的數值 符號因為所在位置不同而代表不同數值。 23.52 10 2×10 1 3×10 0 5×10 -1 5×10 -2 MSB (most significant bit) LSB (least significant bit)

8 Introduction to Computer Science 82008Fall Semester Hexadecimal System  電腦內部的資料適合以二進位來描述,但人卻不 善於使用 0,1 這種長串的數字表示法,因此才折衷 採用十六進位。  十六進位採用 0, 1,…, 9, A, B, C, D, E, F 來表示。  十六進位數值實例:  在 windows 的命令提示字元交談窗中 , 輸入 “dir” 指 令 , 即可看到 “ 磁碟區序號: ”  輸入 “ipconfig /all” 指令,所看到網路卡的 “ 實體位 址 ” (physical address) 欄位

9 Introduction to Computer Science 92008Fall Semester 數值的轉換 10 進制 2 進制 8 進制 16 進制 0000 1111 21022 31133 410044 510155 611066 711177 81000108 10 進制 2 進制 8 進制 16 進制 91001119 10101012A 11101113B 12110014C 13110115D 14111016E 15111117F 16100002010

10 Introduction to Computer Science 102008Fall Semester Binary to Decimal  1110.1101 2 所對應的十進位數為 14.8125 10 1 1 1 0. 1 1 0 1 23232 2121 2020 2 -1 2 -2 2 -3 2 -4 2 3 + 2 2 + 2 1 + 2 -1 + 2 -2 + 2 -4 = 8 + 4 + 2 + 0.5 + 0.25 + 0.0625 = 14.8125

11 Introduction to Computer Science 112008Fall Semester Decimal Integer to Binary  十進位 35 所對應的二進位數為 100011 2 235 217 ………1 28…….. 0 24 22……0 1 100011

12 Introduction to Computer Science 122008Fall Semester Decimal Fraction to Binary  十進位 0.8125 所對應的二進位數為 0.1101 2 0.8125 ×2 1.6250 ×2 1.2500 ×2 0.5000 ×2 1.0000 0.1101

13 Introduction to Computer Science 132008Fall Semester Decimal Fraction to Binary  十進位 0.45 所對應的二進位數為何? 0.45 × 2 = 0.9 0.9 × 2 = 1.8 0.8 × 2 = 1.6 0.6 × 2 = 1.2 0.2 × 2 = 0.4 0.4 × 2 = 0.8 0.011100

14 Introduction to Computer Science 142008Fall Semester Binary & Hexadecimal … d 9 d 8 d 7 d 6 d 5 d 4 d 3 d 2 d 1 ‧ r 1 r 2 r 3 r 4 r 5 r 6 r 7 r 8 r 9 … 2 8 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0 2 -1 2 -2 2 -3 2 -4 2 -5 2 -6 2 -7 2 -8 16 0 16 1 16 -1 16 -2

15 Introduction to Computer Science 152008Fall Semester Binary to Hexadecimal  二進位 10100011.0111001 所對應的十六進位數為 何? 1 0 1 0 0 0 1 1. 0 1 1 1 0 0 1 3A7 0 2

16 Introduction to Computer Science 162008Fall Semester Hexadecimal to Binary  十六進位 1B5.D8 所對應的二進位數為何? 1 B 5. D 8 000110110101.11011000 110110101.11011

17 Introduction to Computer Science 172008Fall Semester Binary & Octal  類似二進位與十六進位的轉換方式,只是在八進 位裡一個數字等於二進位裡三個位數。  怎麼轉換十進位數字到十六進位呢?  電腦處理的資料都會先轉換成二進位數字的表示 方式,然而由於電腦的儲存空間有限,所以一般 會選用有限數目的位元來表示數值。

18 Introduction to Computer Science 182008Fall Semester Outline Data Storage 4. Floating Point 3. Numeric Representation 2. Number Systems 1.

19 Introduction to Computer Science 192008Fall Semester 數值資料表示法  電腦內的二進位數字都只是正整數而已,並沒有 “+” 、 “-” 等符號來表示正、負數,也沒有表示小 數點的符號。  為了解決這些問題,便有人提出幾種不同的負數 與小數表示方法。常見的負數表示法有下列 3 種:  最高位元表示法  1 補數表示法  2 補數表示法

20 Introduction to Computer Science 202008Fall Semester 最高位元表示法 ( 以 8 位元表示整數 ) 位元字串十進位數 000000000 000000011 000000102 …… 11111110254 11111111255 位元字串十進位數 000000000 000000011 …… 01111111127 10000000-0 10000001 …… 11111111-127

21 Introduction to Computer Science 212008Fall Semester 1 補數 (1’s Complement) 表示法  一個數的負數即是將該數的 1 變 0 、 0 變 1 ,下表 顯示出以 3 bits 所能表示的正負數。 10 進位數值 1 補數表示法 -3100 -2101 110 -0111 0000 1001 2010 3011 Q: 若要表示 4 呢? overflow

22 Introduction to Computer Science 222008Fall Semester 2 補數 (2’s Complement) 表示法  一個數的負數即是 其 1 的補數值再加 1 , 右表顯示出以 3 bits 所能表示的正負數。 10 進位數值 1 補數表示法 2 補數表示法 -4overflow100 -3100101 -2101110 110111 -0111 無 0000 1001 2010 3011

23 Introduction to Computer Science 232008Fall Semester 2 補數 (2’s Complement) 表示法  由右表可知,只有在 0 時,該數 1 的補數值加 1 ,才會使得最高位元 進位;此時捨棄該進 位的位元,使得該數 (0) 的正負數表示法相 同,免去有兩種 0 表示 法的困擾。  最左邊的位元可看出 正負,其餘位元則代 表數值大小。 10 進位數值 1 補數表示法 2 補數表示法 -4overflow100 -3100101 -2101110 110111 -0111 無 0000 1001 2010 3011

24 Introduction to Computer Science 242008Fall Semester 2 補數 (2’s Complement) 表示法  正數由 1~3 ,總共有 3 個,負數由 -1~-4 ,總 共 4 個,再加上 1 個 0, 所以 3 bits 共可以表示 2 3 =8 個數。  2 補數表示法除了免去 兩個 0 的表示法外,另 一個好處是可以用加 法運算取代減法運算。 10 進位數值 1 補數表示法 2 補數表示法 -4overflow100 -3100101 -2101110 110111 -0111 無 0000 1001 2010 3011

25 Introduction to Computer Science 252008Fall Semester Practice  用 8 bits 表示, 41 10 的二進位表示法為?  00101001  用 8 bits 表示, -41 的 1 補數表示法為?  11010110  用 8 bits 表示, 2 補數表示法 11010111 ,其十進位 數字為?  -41

26 Introduction to Computer Science 262008Fall Semester 2 補數表示法的兩正數相加 進位 → 1 1600010000 +)2400011000 4000101000

27 Introduction to Computer Science 272008Fall Semester 2 補數表示法的一正一負相加,且結果為正 進位 → 1111 -1611110000 +)2400011000 800001000

28 Introduction to Computer Science 282008Fall Semester 2 補數表示法的一正一負相加,且結果為負 進位 → 1600010000 +)-2411101000 -811111000

29 Introduction to Computer Science 292008Fall Semester 2 補數表示法的兩負數相加 進位 → 111 -1611110000 +)-2411101000 -4011011000

30 Introduction to Computer Science 302008Fall Semester 2 補數表示法的兩正數相加,結果超過正數 儲存範圍 進位 → 1111 12001111000 +)900001001 12910000001 overflow

31 Introduction to Computer Science 312008Fall Semester 2 補數表示法的兩負數相加,結果小於負數 儲存範圍 進位 → 1 -12010001000 +)-911110111 -12901111111 overflow

32 Introduction to Computer Science 322008Fall Semester 2 補數法運算方式說明  假設有 n bits 且 x 是一正數,則 -x 的 2 補數表示法為 2 n -x  Ex: -41 的 2 補數表示法為 2 8 -41 2828 100000000 -)4100101001 -4111010111

33 Introduction to Computer Science 332008Fall Semester 2 補數法運算方式說明  正數 + 正數  負數 (-x) + 負數 (-y)  -x 在二補數表示值為 2 n - x  -y 在二補數表示值為 2 n - y 2 n -x + 2 n -y = 2 n + (2 n -(x+y)) 進位 -(x+y) 的 2 補數表示法

34 Introduction to Computer Science 342008Fall Semester 2 補數法運算方式說明  正數 (x) + 負數 (-y)  -y 的 2 補數表示法為 2 n - y → 2 n + x - y  (1) x ≥ y : x-y 為正數或 0 ; 2 n 為進位  (2) x < y : 2 n + x - y = 2 n - (y - x) -(y-x) 的 2 補數表示法

35 Introduction to Computer Science 352008Fall Semester Outline Data Storage 4. Floating Point 3. Numeric Representation 2. Number Systems 1.

36 Introduction to Computer Science 362008Fall Semester 浮點數 (Floating Point) 表示法  囿限於記憶體空間,所以沒辦法用有限的 0 與 1 來 儲存所有的實數。  以 800000012 來說,若是省略尾巴的 12 ,跟原來的 數值差異不大;但以 0.000000012 來看,若是同樣 省略尾巴的 12 ,則就差之千里了。  因此必須在盡量精準的要求下表示出數值的近似 值,浮點數表示法就是一種解決方法:  數值正規化 (Normalization)  依浮點數表示規則轉換資料

37 Introduction to Computer Science 372008Fall Semester Normalization  正規化是調整數值小數點的位置,使小數點左邊 僅剩一個非零數字,其餘全部移到小數點右邊  8123699 → 8.123699×10 6  0.000007423 → 7.423×10 -6  12.25 10 = 1100.01 2 → 1.10001×2 3  正規化的目的是保留數值中比較重要的資訊;在 正規化後重要的部分有  正負符號、指數部份以及小數部份 (-1) S ×(1.F) × 2 E

38 Introduction to Computer Science 382008Fall Semester 浮點數表示規則  不同的 CPU 和 FPU 有其各自的浮點數表示法,但 一般較常採用 IEEE 所訂定的浮點數表示法標準 (IEEE Standard 754) ,分成下列二種: (-1) S ×(1.F) × 2 E 美國電機電子工程師學會 (Institute of Electrical and Electronic Engineers, short to IEEE) 18 bits23 bits 單倍精準數 (single-precision) 符號 指數部份小數部份 111 bits52 bits 雙倍精準數 (double-precision)

39 Introduction to Computer Science 392008Fall Semester 單倍精準數  符號: 0 代表正數, 1 則是負數  指數部份採用偏移的方式,以 127 為偏移值,稱為 過剩 127(Excess-127) 表示法  小數部份從標準化的小數點後開始存起,不夠的 位元部份補 0 (-1) S ×(1.F) × 2 E 255 254 … 129 128 127 126 125 … 1 0 128 127 … 2 1 0 -1 -2 …-126 -127

40 Introduction to Computer Science 402008Fall Semester Practice  12.25 10 = 1100.01 2 → 1.10001×2 3  符號為 0  指數為 3 ,以 Excess-127 表示則為 130 (= 3 + 127) , 轉換成 8 bits 的二進位表示法為 10000010 2  小數為 100010…0  完整的表示法為 010000010100010…0 (-1) S ×(1.F) × 2 E 18 個 0

41 Introduction to Computer Science 412008Fall Semester Practice  在 IEEE-754 標準下,單倍精準數 10000010100101000110000000000000 是二進位多 少?  符號為 1 ,是負數  指數部分是 5 10 ,減去 127 得 -122  小數部份是 0010100011  所代表的數值是 -1.0010100011×2 -122  驗算網址 :  http://babbage.cs.qc.edu/IEEE-754/Decimal.html (-1) S ×(1.F) × 2 E

42 Introduction to Computer Science 422008Fall Semester IEEE-754 單倍精準數  指數部分的 -127(00000000) 和 +128(11111111) 保留 做為特殊用途。 (-1) S ×(1.F) × 2 E 指數部分小數部份代表意義 00±0 0 非0非0 非正規形式 1~2 8 -2 任意正規形式 2 8 -10 ± 無窮大 2 8 -1 非0非0 不是一個數

43 Introduction to Computer Science 432008Fall Semester IEEE-754 單倍精準數  0 的表示法有兩個  最小的正數為 00000000100000000000000000000000 其數值為 2 -126 ( ≒ 1.175×10 -38 )  最大的正數則為 01111111011111111111111111111111 其數值為 (2-2 -23 )×2 127 ( ≒ 3.402×10 38 )

44 Introduction to Computer Science 442008Fall Semester Outline Data Storage 4. Floating Point 3. Numeric Representation 2. Number Systems 1.

45 Introduction to Computer Science 452008Fall Semester Storing Text  要在電腦內處理文字資料,就必須先瞭解文字資 料在電腦中的表示方法;而數值資料在電腦內部 是以二進位的形式來表示。  我們輸入電腦的文字資料,亦會被轉換成二進位 碼的形式儲存;同樣地,想將文字資料輸出時, 電腦亦會將二進位碼轉換成對應的字元符號,再 藉由輸出設備顯示或列印出來。  這種將文字資料轉換成二進位碼的系統就稱為編 碼系統。

46 Introduction to Computer Science 462008Fall Semester Text Representation  我們把每一個英文字母編上一個號碼,並建成一 套編碼表。  這個編碼表可以存放在鍵盤內的電路上。  當按下一個鍵時,鍵盤的電路便依表格的規定, 把該鍵對應的二進位碼送往電腦主機 ( 如按下 [A] 鍵,鍵盤便會把相對應碼 “65” 的二進位數 01000001 傳到主機 ) 。  想將文字資料輸出時,編碼系統亦會將二進位碼 轉換成對應的字元符號,再藉由輸出設備顯示或 列印出來。

47 Introduction to Computer Science 472008Fall Semester Common Encoding Systems  常見的英文編碼系統有 ASCII 、 ISO8859 和 EBCDIC 。  常見的繁體中文編碼系統有 Big5 以及 CP950 。  常見的簡體中文編碼系統有 GB 2312 、 GBK 和 GB 18030 。  全球統一的編碼系統 Unicode 。

48 Introduction to Computer Science 482008Fall Semester ASCII Code  ASCII 是美國標準資訊交換碼 (American Standard Code for Information Interchange) 的縮寫,包含英 文大、小寫字母,阿拉伯數字、控制字元以及各 種符號等。  ASCII 碼是以 7 bits (2 7 =128 個 ) 來表示,每一個碼 都對應一個字元,在電腦中是以 1 byte 來儲存。  由於 1 byte 可存 2 8 個資料,所以擴充 ASCII ,多了 128 擴充符號字元,可用來用來繪製表格的格線或 印出特殊的字母、符號等。

49 Introduction to Computer Science 492008Fall Semester ASCII Code :第一部分  第一部分由 00 到 31 共 32 個,一般 用來通訊或作為 控制之用。有些 字元可顯示於螢 幕,有些雖無法 顯示,但能看到 其效果。 參考網頁

50 Introduction to Computer Science 502008Fall Semester ASCII Code :第二部分  第二部分是由 32 到 127 共 96 個,這 96 個 字元是用來表示阿拉 伯數字、英文字母大 小寫和底線、括號等 符號,都可以顯示在 螢幕上。 參考網頁

51 Introduction to Computer Science 512008Fall Semester ASCII Code :第三部分  第三部分由 128 到 255 共 128 個字元,一般稱 為擴充字元。這些字 元是用來表示框線、 音標和其他歐洲非英 語系的字母。 參考網頁

52 Introduction to Computer Science 522008Fall Semester EBCDIC Code  EBCDIC Code 的是 Extended Binary Code Decimal Interchange Code 的縮寫,是美國 IBM 公司所制定 的編碼系統,主要使用在 IBM 一部份大型主機上。  EBCDIC 的每一個字元是由 8 個位元所組成,共有 2 8 種組合。

53 Introduction to Computer Science 532008Fall Semester 中文電腦編碼系統  在電腦中, 8 bits 就足以表示所有的英文字母、阿 拉伯數字及許多特殊符號,但卻不足以表示所有 的中文字;因此中文字是以 2 bytes 來編碼。  目前比較常見的中文電腦編碼系統有 Big5 和 CP950 。

54 Introduction to Computer Science 542008Fall Semester Big5 Code  由資策會工業局聯合 13 家業者於 1984 年所共同制 定的編碼系統。  包含了 5,401 個常用字、 7,652 個次常用字及 408 個 符號 ( 含標點符號、注音符號、單位符號 ……) , 總共 13,461 個字。  2003 年經濟標表準檢驗局委託重整得到 Big5-2003 , 經增添定義後共有 19,782 個中文字。

55 Introduction to Computer Science 552008Fall Semester Big5 Code  雖然 Big5-2003 有 19,782 個中文字,但仍有些廣泛 採用的字沒有收錄到 Big5 之中。  常見的人名用字,如游錫 “ 堃 ” 、王建 “ 煊 ” 、張 “ 栢 ” 芝、陶 “ 喆 ” 等均沒有收錄。 http://tw.news.yahoo.com/article/url/d/a/ 080721/5/13mgo.html http://tw.news.yahoo.com/article/url/d/a/ 080721/17/13mfq.html

56 Introduction to Computer Science 562008Fall Semester 全球統一的編碼系統  現在全世界有許多語言流通,甚至有些語言還會 有多種編碼系統 ( 例如繁體中文就有 Big5 、 CP950…) ,使得全世界有相當多種編碼系統。  這也衍生出一個問題,當使用不同編碼的電腦要 交換資料時,由於沒有共用的編碼系統,以致無 法解讀彼此的文字。  例如我們平時在瀏覽網頁或收信時,有時就會發 現網頁或信的內容全部是亂碼。

57 Introduction to Computer Science 572008Fall Semester Unicode ( 統一碼、萬國碼 )  Unicode provides a unique number for every character,  no matter what the platform,  no matter what the program,  no matter what the language. (http://www.unicode.org)  1991 年美國資訊科技公會和 ISO 國際標準化組織 合作制定 2 bytes 的 Unicode 1.0 (Universal Character Encoding) ,目前更新到 5.1 版,包含超過 十萬個字元。

58 Introduction to Computer Science 582008Fall Semester Unicode  已發展出多種編碼方式: UTF-8 、 UTF-16 及 UTF- 32 等 (Unicode Translation Format, UTF) ,分別以 8 位元、 16 位元及 32 位元為基本單元的編碼方式。  UTF-8 通行於網頁和 E-mail , UTF-16 為 Java 及 Windows 所採用,而 UTF-32 則常見在一些 Unix 系 統。  例如在 UTF-16 編碼方式中,林 (6797) 英 (82F1) 志 (5FD7) 。

59 Introduction to Computer Science 592008Fall Semester Unicode Query  Windows 提供的造字程式  開始 → 附屬應用程式 → True Type 造字程式  透透過網頁 ISO 10646 漢字查詢系統 ISO 10646 漢字查詢系統  香港理工大學架的網站,從首頁點選「漢字查詢系 統」就會進入查詢介面。只要把想找的字的一部份 打進去,它就會把所有含有這個部分的字列出來。  透過行政院主計處查詢網頁行政院主計處查詢  Free Online Unicode Character Map Free Online Unicode Character Map


Download ppt "12008Fall Semester Introduction to Computer Science Chapter 2 Data Representation and Storage Speaker: Ying Chih Lin Department of Applied Mathematics."

Similar presentations


Ads by Google