Data representation (การแทนข้ อมูล) ระบบตัวเลขและการแปลงเลขฐาน (Number System and Conversion) ระบบตัวเลข (Number System) ฐาน (Base or radix) - ค่าที่จะบอกให้ทราบว่าเลข ในระบบนั้นๆ จะมีสญ ั ลักษณ์ใช้ท้ งั หมดกี่ตวั หรื อ.
Download ReportTranscript Data representation (การแทนข้ อมูล) ระบบตัวเลขและการแปลงเลขฐาน (Number System and Conversion) ระบบตัวเลข (Number System) ฐาน (Base or radix) - ค่าที่จะบอกให้ทราบว่าเลข ในระบบนั้นๆ จะมีสญ ั ลักษณ์ใช้ท้ งั หมดกี่ตวั หรื อ.
Data representation (การแทนข้ อมูล) ระบบตัวเลขและการแปลงเลขฐาน (Number System and Conversion) ระบบตัวเลข (Number System) ฐาน (Base or radix) - ค่าที่จะบอกให้ทราบว่าเลข ในระบบนั้นๆ จะมีสญ ั ลักษณ์ใช้ท้ งั หมดกี่ตวั หรื อ มีค่าตัวเลข โดดๆ (absolute value) กี่ตวั ระบบจำนวนที่ใช้ในทำงคอมพิวเตอร์ ประกอบด้วย ระบบเลขฐำนสอง (Binary Number System) ประกอบด้วยเลข 0,1 ระบบเลขฐำนแปด (Octal Number System) ประกอบด้วยเลข 0 - 7 ระบบเลขฐำนสิ บ (Decimal Number System) ประกอบด้วยเลข 0 - 9 ระบบเลขฐำนสิ บหก (Hexadecimal Number System) ประกอบด้วย ตัว เลข 0 - 9 และ A - F decimal 724.5 binary - The decimal number system uses the radix 10. The 10 symbols are 0,1,2,3,4,5,6,7,8,9. 7 x 102 + 2 x 101 + 4 x 100 + 5 x 10-1 - The binary system uses the radix 2. The two digits symbols used are 0 and 1. 1.) 101101 1x25 + 0x24 + 1x23 + 1x22 + 0x21 + 1x20 = 45 2.) กำรแปลง (101101)2 เป็ นเลขฐำนสิ บ บิทซ้ำยสุ ด คูณด้วย 2 และบวกบิทถัดไป(2 x 1) + 0 = 2 คูณด้วย 2 และบวกบิทถัดไป(2 x 2) + 1 = 5 คูณด้วย 2 และบวกบิทถัดไป(2 x 5) + 1 = 11 คูณด้วย 2 และบวกบิทถัดไป(2 x 11) + 0 = 22 คูณด้วย 2 และบวกบิทถัดไป(2 x 22) + 1 = 45 ดังนั้น (101101)2 = (45)10 Binary Data Representation octal - The octal system uses the radix 8. The eight symbols used are 0,1,2,3,4,5,6,7 736.4 = 7 x 82 + 3 x 81 + 6 x 80 + 4 x 8-1 = 7 x 64 + 3 x 8 + 6 x 1 + 4/8 = 478.5 hexadecimal - The hexadecimal system uses the radix 16 The 16 symbols used are 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F F3 = F x 161 + 3 x 160 = 15 x 16 + 3 x 1 = 243 เขียนเป็ นสู ตรได้ดงั นี้ N = ai rii Conversion - Conversion from decimal to binary, octal, and hexadecimal Example Conversion of decimal 41.6875 into binary Integer = 41 Fraction = 0.6875 41 0.6875 x 2 = 1.3750 20 1 0.3750 x 2 = 0.7500 10 0 0.7500 x 2 = 1.5000 5 0 0.5000 x 2 = 1.0000 2 1 1 0 0 1 (41)10 = (101001) 2 (0.6875)10 = (0.1011) 2 (41.6875)10 = (101001.1011)2 = 0 x 22 + 0 x 21 + 1 x 20 = 32 + 0 + 8 + 0 + 0 = 41 101001 + +1 5 4 3 1x2 +0x2 +1x2 1 2 7 5 4 3 1 0 1 0 1 1 1 1 0 1 1 0 0 0 1 1 A F 6 3 Binary, octal, and hexadecimal conversion การคานวณในระบบเลขฐานต่างๆ การบวกเลขฐาน เลขฐานสอง 1010.1 10.5 + 1 0 1 0 . 1 + 10101.0 21.0 เลขฐานแปด 15.5 625 14.4 + 5 + 3 2 . 1 125 เลขฐานสิ บหก เลขฐานสิ บ 10.5 13. 12. 26. การคานวณในระบบเลขฐานต่างๆ การลบเลขฐาน เลขฐานสอง 101011 010110 010101 เลขฐานแปด 5124 2647 2255 เลขฐานสิ บหก FC2 6 -A4 B8 = ? การลบโดยใช้ Complements Complements แบ่งออกเป็ น 2 ประเภท คือ - r’s complement - (r - 1)’s complement การคานวณหาค่า complements ของตัวเลขฐาน r ใดๆ สามารถหาจากสูตรดังนี้คอ ื r ' s complement = r n - N (r 1)' s complement = r n - N - 1 ตัวอย่ำง กำรหำ 2’s complement ของเลขฐำนสอง 100110 r’s complement = rn – N 2’s complement = 26 – 38 = 64 – 38 = 26 2’s complement = (011010)2 1’s complement = rn – N – 1 = 64 – 38 – 1 = 25 1’s complement = (011001)2 ข้อสังเกตในกำรหำ complement ของเลขฐำนสองอย่ำงง่ำย คือ เลชฐำนสอง (1 0 0 1 1 0)2 1’s complement (0 1 1 0 0 1)2 2’s complement (0 1 1 0 1 0)2 ตัวอย่ำง กำรหำ complement ของเลขฐำนอื่นๆ เลขฐำนสิ บ = 34974 9’s complement = 65025 10’s complement = 65026 เลขฐำนแปด 7’s complement 8’s complement = = = เลขฐำนสิ บหก = 15’s complement = 16’s complement = 115 662 663 A719C 58E63 58E64 The subtraction of two n-digit unsigned numbers M - N by using r’s complement • M >= N (M = 72532, N = 13250) radix 10 M = 72532 10’s complement of N = + 86750 Sum = 159282 Discard end carry 105 = - 100000 Answer = 59282 •M < N (M = 13250, N = 72532) M = 13250 10’s complement of N = + 27468 Sum = 40718 There is no end carry Answer is negative 59282 (10’s complement of 40718) Example 10 - 6 = ? 6 - 10 = ? Subtraction with binary number. Using X = 1010100 and Y = 1000011 •X - Y X = 1010100 2’s complement of Y = + 0111101 Sum = 10010001 Discard end carry 2^7 = - 10000000 Answer X - Y = 0010001 •Y - X Y = 1000011 2’s complement of X = + 0101100 Sum = 1101111 There is no end carry Answer is negative 0010001 (2’s complement of 110 การคูณ เลขฐานแปด 17x 13 55 17 245 การหาร เลขฐานแปด 125 14 1774 14 37 30 74 74 Multiplication Algorithm 23 19 437 10111 x 10011 10111 10111 00000 + 00000 10111 110110101 Multiplicand Multiplier Product Numerical Example for Binary Multiplier Multiplicand B = 10111 E Multiplier in Q 0 Qn = 1 ; add B First partial product 0 Shift right EAQ 0 Qn = 1 ; add B Second partial product 1 Shift right EAQ 0 Qn = 0 ; shift right EAQ 0 Qn = 0 ; shift right EAQ 0 Qn = 1 ; add B Fifth partial product 0 Shift right EAQ 0 Final product in AQ = 0110110101 A 00000 10111 10111 01011 10111 00010 10001 01000 00100 10111 11011 01101 Q 10011 SC 101 11001 100 01100 011 10110 010 01011 001 10101 000 Integer signed numbers representation When an integer binary number is positive, the sign is represented by 0 and the magnitude by positive binary number. When the number is negative, the sign is represented by 1 and the rest of number may be represented in one of three possible ways: 1. Signed - magnitude representation 2. Signed - 1’s complement representation 3. Signed - 2’s complement representation Example number 14 +14 0 0001110 -14 In signed - magnitude representation 1 0001110 -14 In signed - 1’s complement 1 1110001 -14 In signed - 2’s complement 1 1110010 sign bit CPU Data Types รหัสข้อมูล (Data Representation) หมำยถึง รหัสที่ใช้แทน ตัวเลข ตัวอักษร สัญลักษณ์ต่ำงๆ ที่ประกอบอยูใ่ นคำสัง่ และ ข้อมูล เพื่อใช้ในกำรประมวลผล สำมำรถแบ่งได้ 2 ประเภทคือ - รหัสภำยในระบบคอมพิวเตอร์ (Internal Code) เป็ นรหัสที่ใช้ แทนข้อมูลในหน่วยควำมจำของคอมพิวเตอร์ เช่น -รหัส BCD - Binary Code Decimal -รหัส EBCDIC - Extended Binary Coded Decimal Interchange Code - รหัส ASCII - American Standard Code for Information Interchange - รหัส สมอ. - รหัส Unicode - รหัสภำยนอกระบบคอมพิวเตอร์ (External Code) เป็ นรหัสที่ พัฒนำสำหรับบันทึกข้อมูลนอกเครื่ องคอมพิวเตอร์ เช่นรหัสที่ ใช้กบั บัตรเจำะรู รหัสภำยในคอมพิวเตอร์ แทนได้กบั สภำวะ ของกระแสไฟฟ้ ำ ตำมจำนวนสำยสัญญำณ เช่น ถ้ำมี สำยสัญญำณ 2 เส้น ก็สำมำรถสร้ำงรหัสแทนข้อมูลได้ 4 ค่ำ (คิดจำก 22 ) คือ รหัสแทนข้อมูลอื่น ๆ • BCD (Binary-Coded Decimal) • ASCII (American Standard Code for Information Interchange) • Binary Gray Code • Excess-3 • Excess-3 Gray • EBCDIC (Extended BCD Interchange Code) CPU Data Types The message “Hello.” in ASCII Error Detection Codes (รหัสตรวจสอบความผิดพลาด) การส่ งผ่านข้อมูลระหว่างสื่ อบันทึกข้อมูลต่างๆ อาจมีขอ้ มูลบางส่ วนผิดพลาดไป เช่น เปลี่ยนค่าจาก 1 เป็ น 0 หรื อ 0 เป็ น 1 ซึ ่ ง เป็ น ผลกระทบ จากสภาพ แวดล้อมภายนอก เช่น มีสญ ั ญาณรบกวนซึ่ งสามารถ ตรวจสอบความผิดพลาดได้ โดยกาหนด bit พิเศษ ขึ้นมา 1 bit เรี ยกว่า parity bit bit ตรวจสอบนี้เป็ น เลขฐานสอง 1 ตัว ที่ใช้ในการรวมเข้ากับกลุ่มของ เลขฐานสองที่เป็ นข้อมูล เพื่อทาให้ผลรวมของตัวเลข ทั้งหมด (1 หรื อ 0) เป็ นจานวนเลขคี่ (odd parity) หรือเป็ นจานวนเลขคู่ (even parity) ตัวอย่าง Parity bit generation Message P(odd) 000 1 001 0 010 0 100 0 110 1 101 1 111 0 0 1 1 1 0 0 1 P(even) The ASCII codes for the letters A and F adjusted for odd parity Data Types The data types may be classified as being one of the following categories: 1. numbers used in arithmetic computations 2. letters of alphabet used in data processing 3. other discrete symbols used for specific purposes All types of data, except binary numbers, are represented in computer registers in binary-coded form. Summary • Numeric data is stored using integer, real number, and floating point formats. • Characters are converted to numbers by means of a coding table. • Boolean vales can have only two values, true and false. • Programs often need to define and manipulate data in larger and more complex units than primitive CPU data types.