Upcoming SlideShare
Loading in …5
×

# Number Systems

8,425 views
8,165 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
• Be the first to comment

• Be the first to like this

No Downloads
Views
Total views
8,425
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
110
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Number Systems

1. 1. Unit 4: The Number Systems and Data Representation
2. 2. Problem-solving using “computers” <ul><li>Computers solve “computable” problems </li></ul>A Problem Describing The Problem in Math. “ Computing” The Corresponding Math. Problem Returning The Result Solution To The Problem Human problem-solving v.s. computer-based problem-solving
3. 3. Transformation input think act input compute act   Binary system
4. 4. The Number System <ul><li>A number system is to enumerate the “states” of something </li></ul><ul><ul><li>For example, money, days, month, year, minutes, hours, … </li></ul></ul><ul><li>In human’s world, we have very good tools   </li></ul>…which counts 10 states
5. 5. The Needs of Number Systems <ul><li>However, there are some states are not Decimal. For example, </li></ul><ul><ul><li>Days and weeks </li></ul></ul><ul><ul><li>Minutes and hours </li></ul></ul><ul><ul><li>Months and year </li></ul></ul><ul><li>We need convenient representations for different “nature states” </li></ul>
6. 7. Egyptian numerals (10-based) Source: http://www-gap.dcs.st-and.ac.uk/~history
7. 8. Babylonians numerals (60-based) Source: http://www-gap.dcs.st-and.ac.uk/~history
8. 9. 1*60 3 + 57*60 2 + 46*60 + 40*60 0 = 424000 Source: http://www-gap.dcs.st-and.ac.uk/~history
9. 10. Chinese numerals (10-based) 4359 45698 Source: http://www-gap.dcs.st-and.ac.uk/~history 壹貳參肆伍陸柒捌玖拾佰仟萬億兆京…
10. 11. The Number Systems <ul><li>A number system specifies how/what “numbers” represent and operate. </li></ul><ul><li>A K-based number system uses N different symbols to represent N different entities or “states” </li></ul><ul><li>Each symbol is a “digit” </li></ul><ul><li>Multiple digits are used for describing M>N states </li></ul>
11. 12. K-based Number System <ul><li>2-based: ON/OFF; Yes/No; T/F; 1/0; etc. </li></ul><ul><li>3-based: A/B/C; True/False/Unknown; 0/1/2; etc. </li></ul><ul><li>… </li></ul><ul><li>7-based: Mon/Tue/Wes/Thu/Fri/Sat/Sun; 0/1/2/3/4/5/6; etc. </li></ul><ul><li>8-based: ; 0/1/2/3/4/5/6/7 </li></ul>
12. 13. K-based Number System <ul><li>10-based:  /  /  /  /  /  /  /  /  /  ; 0/1/2/3/4/5/6/7/8/9; etc. </li></ul><ul><li>16-based:  /  /  /  /  /  /  /  /  /  /  /  ; 0/1/2/3/4/5/6/7/8/9/A/B/C/D/E/F; etc. </li></ul>State-1 State-2 State-3 State-4 State-5 State-6 State-7 State-8 State-9 State-10 State-11 State-12 State-13 State-14 State-15 State-16
13. 14. K-based Number System <ul><li>In the 16-based number system: </li></ul><ul><li>0/1/2/3/4/5/6/7/8/9/A/B/C/D/E/F; etc. </li></ul>Why not “ 1 0 ” 2 digits
14. 15. More States <ul><li>In a 6-based number system </li></ul><ul><li> : state 1 (empty state) (0) </li></ul><ul><li> : state 2 (1 st ) </li></ul><ul><li> : state 3 (2 nd ) </li></ul><ul><li> : state 4 (3 rd ) </li></ul><ul><li> : state 5 (4 th ) </li></ul><ul><li> : state 6 (5 th ) </li></ul><ul><li> : state 7 (6 th ) </li></ul><ul><li> : state 8 (7 th ) </li></ul><ul><li> : state 9 (8 th ) </li></ul><ul><li> : state 10 (9 th ) </li></ul><ul><li> : state 11 (10 th ) </li></ul><ul><li> : state 12 (13 th ) </li></ul><ul><li>… </li></ul>
15. 16. More States <ul><li>In a 6-based number system </li></ul><ul><li>0: state 1 (empty state) (0) </li></ul><ul><li>1: state 2 (1 st ) </li></ul><ul><li>2: state 3 (2 nd ) </li></ul><ul><li>3: state 4 (3 rd ) </li></ul><ul><li>4: state 5 (4 th ) </li></ul><ul><li>5: state 6 (5 th ) </li></ul><ul><li>10: state 7 (6 th ) </li></ul><ul><li>11: state 8 (7 th ) </li></ul><ul><li>12: state 9 (8 th ) </li></ul><ul><li>13: state 10 (9 th ) </li></ul><ul><li>14: state 11 (10 th ) </li></ul><ul><li>15: state 12 (13 th ) </li></ul><ul><li>20: state 13 (14 th ) </li></ul><ul><li>… </li></ul>
16. 17. The Number of Cases <ul><li>Generally, in a K-based number system, </li></ul><ul><ul><li>1 digit describes K 1 cases </li></ul></ul><ul><ul><li>2 digits describes K 2 cases </li></ul></ul><ul><ul><li>3 digits describes K 3 cases </li></ul></ul><ul><ul><li>N digits describes K N cases </li></ul></ul><ul><li>For example, </li></ul><ul><ul><li>2 3-based (0,1,2) digits: 00, 01, 02, 10, 11, 12, 20, 21, 22 (3 2 =9 cases) </li></ul></ul><ul><ul><li>3 10-based (0,1,2,..,9) digits: 000, 001, 002, 003, 004, 005, 006,007, 008, 009, 010, 011,…,998, 999 (10 3 =1000 cases) </li></ul></ul>
17. 18. K-based vs The Decimal system (10-based) <ul><li>In general, any K-based number N can be expressed as </li></ul><ul><li>Usually, N k is denoted as </li></ul><ul><ul><li>(A p-1 A p-2 ….A 1 A 0 . A -1 A -2 ….A -q ) k </li></ul></ul><ul><ul><li>A p-1 :Most Significant Digit, MSD </li></ul></ul><ul><ul><li>A -q :Least Significant Digit, LSD </li></ul></ul>
18. 19. Source: 計算機概論 , 王孝熙 著 , 東華書局 .
19. 20. Using octal and hex numbers <ul><li>Computers use binary, but the numbers are too long and confusing for people </li></ul><ul><li>Translation between binary and octal or hex is easy </li></ul><ul><ul><li>One octal digit equals three binary digits </li></ul></ul><ul><ul><li>101101011100101000001011 </li></ul></ul><ul><ul><li>5 5 3 4 5 0 1 3 </li></ul></ul><ul><ul><li>One hexadecimal digit equals four binary digits </li></ul></ul><ul><ul><li>101101011100101000001011 </li></ul></ul><ul><ul><li>B 5 C A 0 B </li></ul></ul>
20. 21. Source: 計算機概論 , 王孝熙 著 , 東華書局 .
21. 22. Source: 計算機概論 , 王孝熙 著 , 東華書局 .
22. 23. Source: 計算機概論 , 王孝熙 著 , 東華書局 .
23. 24. Source: 計算機概論 , 王孝熙 著 , 東華書局 .
24. 25. Source: 計算機概論 , 王孝熙 著 , 東華書局 .
25. 26. Source: 計算機概論 , 王孝熙 著 , 東華書局 .
26. 27. The binary number system <ul><li>In modem computers, the binary number system is easier to be implemented. </li></ul><ul><li>Currently, they are implemented in silicon chips (VLSI) </li></ul><ul><ul><li>Circuits for computation </li></ul></ul>01111 00011 10010 15 3 + 10 + 2 18
27. 28. The binary number system <ul><li>The binary (base 2 or 2-based) number system uses two “binary digits, ” (abbreviation: bits) -- 0 and 1 </li></ul><ul><li>A bit is a single two-valued quantity: yes or no, true or false, on or off, high or low, good or bad </li></ul><ul><li>One bit can distinguish between two cases: T, F (True/False; Yes/No) </li></ul><ul><li>Two bits can distinguish between four cases: TT, TF, FT, FF </li></ul><ul><li>Three bits can distinguish between eight cases: TTT, TTF, TFT, TFF, FTT, FTF, FFT, FFF </li></ul><ul><li>In general, n bits can distinguish between 2 n cases </li></ul><ul><li>A byte is 8 bits, therefore 2 8 = 256 cases </li></ul>
28. 29. Data Processing in Computers with the Binary System <ul><li>Data processing in binary computers </li></ul><ul><ul><li>Numeric data: +2, -5.5, 50000.235698, etc. </li></ul></ul><ul><ul><li>Alphanumeric data: characters, name, address, telephone number, etc. </li></ul></ul><ul><li>Two main tasks: </li></ul><ul><ul><li>All data are converted into proper binary formats (bits) </li></ul></ul><ul><ul><li>Rules for counting and computing these bits </li></ul></ul>
29. 30. Part I: Numeric Data <ul><li>Numeric data: </li></ul><ul><ul><li>integers, real </li></ul></ul><ul><ul><li>positive, negative </li></ul></ul><ul><li>Representation of numeric data </li></ul><ul><ul><li>Sign-magnitude </li></ul></ul><ul><ul><li>1’s Complement </li></ul></ul><ul><ul><li>2’s Complement </li></ul></ul><ul><li>Negative/Positive in a n-bit integer I </li></ul><ul><li>I=(A n-1 A n-2 ….A 1 A 0 ) 2 </li></ul><ul><ul><li>A n-1 = “0”, I is positive </li></ul></ul><ul><ul><li>A n-1 =“1”, I is negative </li></ul></ul>
30. 31. Sign-Magnitude <ul><li>An n-bit integer I= </li></ul><ul><ul><li>Min: 00000…0 (n-1 0’s)=0 </li></ul></ul><ul><ul><li>Max: 11111…1 (n-1 1’s)=2 n-1 -1 </li></ul></ul><ul><ul><li>+0= (0000) 2 vs –0=(1000) 2 </li></ul></ul><ul><ul><li>+3= (0011) 2 vs –3=(1011) 2 </li></ul></ul><ul><li>Disadvantages </li></ul><ul><ul><li>2 different 0’s (+0/-0 ） </li></ul></ul><ul><ul><li>Not easy to be implemented in simple circuits (usually adder) </li></ul></ul>
31. 32. 1’s Complement <ul><li>An n-bit integer I= </li></ul><ul><ul><li>For positive integers: the same as that in sign-magnitude </li></ul></ul><ul><ul><li>For negative integers: complement their positive representations </li></ul></ul><ul><ul><li>For example: 4-bit integers: </li></ul></ul><ul><ul><ul><li>+3=(0011) 2 ， -3=(1100) 2 </li></ul></ul></ul><ul><ul><ul><li>+0=(0000) 2 ， -0=(1111) 2 </li></ul></ul></ul><ul><li>Disadvantages </li></ul><ul><ul><li>2 different 0’s (+0/-0 ） </li></ul></ul><ul><ul><li>Easy to be implemented in simple circuits (usually adder) but not as efficient as 2’s complement </li></ul></ul>
32. 33. 2’s Complement <ul><li>An n-bit integer I= </li></ul><ul><ul><li>For positive integers: the same as that in sign-magnitude </li></ul></ul><ul><ul><li>For negative integers: find their 1’s complement + 1 </li></ul></ul><ul><ul><ul><li>For example: 4-bit integers: </li></ul></ul></ul><ul><ul><ul><li>+3 = (0011) 2 ， -3 = (1100) 2 +1 = (1101) 2 </li></ul></ul></ul><ul><ul><ul><li>+0 = (0000) 2 ， -0 = (1111) 2 + 1 = (0000) 2 </li></ul></ul></ul><ul><ul><ul><li>There is only one “0” </li></ul></ul></ul>
33. 34. Comparison: 4-bit representation of integers
34. 35. Range of integers Suppose that we use a byte (8-bit) to represent an integer (00000000) 2 (00000000) 2 (10000000) 2 =-128 (01111111) 2 =+127 2‘s complement (11111111) 2 (00000000) 2 (10000000) 2 =-127 (01111111) 2 =+127 1‘s Complement (10000000) 2 (00000000) 2 (11111111) 2 =-127 (01111111) 2 =+127 Sign-Magnitude -0 +0 Max. (-) Max. (+) -(2 n-1 )~0 0~2 n-1 -1 2‘s complement -(2 n-1 -1)~0 0~2 n-1 -1 1‘s Complement -(2 n-1 -1)~0 0~2 n-1 -1 Sign-Magnitude Negative numbers Positive numbers
35. 36. Note <ul><li>Representation level </li></ul><ul><li>Implementation level </li></ul>
36. 37. Other Binary Codes for Decimal Numbers <ul><li>BCD code </li></ul><ul><li>2421 code </li></ul><ul><li>Excess-3 code </li></ul><ul><li>84-2-1 code </li></ul><ul><li>4-bit for a number (0~9) </li></ul>
37. 38. 8421 BCD Code <ul><li>In the 8421 Binary Coded Decimal (BCD) representation each decimal digit is converted to its 4-bit pure binary equivalent. </li></ul><ul><li>Each decimal number maps to four bits and is weighted by its bit-position (each bit represents a number, 1, 2, 4, 8) </li></ul><ul><li>BCD code is also called as 8421 code </li></ul>
38. 39. 4221 BCD Code <ul><li>In 4221 BCD code, each bit is weighted by 4, 2, 2 and 1 respectively. </li></ul><ul><li>Unlike BCD coding there are no invalid representations. </li></ul>advantages
39. 40. Excess-3 Code <ul><li>Add 3 for each binary number </li></ul><ul><li>Examples </li></ul><ul><ul><li>Excess-3 code of “2 10 ” = (0010) 2 +(0011) 2 =(0101) 2 </li></ul></ul><ul><ul><li>Excess-3 code of “5 10 ” = (0101) 2 +(0011) 2 =(1000) 2 </li></ul></ul><ul><li>Advantages </li></ul>
40. 41. 84-2-1 Code <ul><li>4-bit for each number (0~9) weighted (left to right) as 8, 4, -2, and -1 。 </li></ul><ul><li>Example </li></ul><ul><ul><li>84-2-1 code of “3” = 0101 (0+4+0+(-1)=3) </li></ul></ul><ul><ul><li>84-2-1 code of “5” =1011 (8+0+(-2)+(-1)=5) </li></ul></ul>
41. 42. Self-complementing code Source: 計算機概論 , 王孝熙 著 , 東華書局 .
42. 43. Gray Codes <ul><li>In pure binary coding or 8421 BCD, counting from 7 (0111) to 8 (1000) requires 4 bits to be changed simultaneously. If this does not happen then various numbers could be momentarily generated during the transition so creating spurious numbers which could be read. </li></ul><ul><li>In gray coding, only one bit changes between subsequent numbers. </li></ul><ul><li>Generating gray codes: start with all 0s and then proceed by changing the least significant bit (LSB) which will bring about a new state. </li></ul><ul><li>Advantages: fast, relatively free from errors. </li></ul>
43. 44. Gray Codes <ul><li>Gray code is not unique, there are many possibilities to generate gray codes </li></ul><ul><li>For example, </li></ul><ul><ul><li>G1={0=00 ， 1=01 ， 2=11 ， 3=10} </li></ul></ul><ul><ul><li>G2={00=10 ， 1=11 ， 2=01 ， 3=00} </li></ul></ul><ul><li>Unique gray code: reflected Gray code </li></ul>
44. 45. <ul><li>從十進位 -> 反射葛雷碼 </li></ul>Source: 計算機概論 , 王孝熙 著 , 東華書局 .
45. 46. 從反射葛雷碼 -> 十進位數字 Source: 計算機概論 , 王孝熙 著 , 東華書局 .
46. 47. Floating-Point Representation <ul><li>Integers are fixed-point numbers in binary computers </li></ul><ul><li>Floating-point literals are written with a decimal point: 8.5 -7.923 5.000 </li></ul><ul><li>Real numbers are represented as “floating-point” numbers in binary system (all computers) </li></ul><ul><li>The representation of floating-point numbers are various in different CPU </li></ul><ul><li>In Intel 80486 CPU </li></ul><ul><ul><li>Single Precision: 32 bits </li></ul></ul><ul><ul><li>Double Precision: 64 bits </li></ul></ul><ul><ul><li>Extended Precision: 80bits </li></ul></ul>
47. 48. Floating-point literals <ul><li>Floating-point numbers may also be written in “scientific notation”– times a power of 10 </li></ul><ul><li>We use E to represent “times 10 to the” </li></ul><ul><li>Example: 4.32E5 means 4.32 x 10 5 </li></ul>http://www.nuvisionmiami.com/books/asm/workbook/floating_tut.htm
48. 49. <ul><li>範例 </li></ul>Source: 計算機概論 , 王孝熙 著 , 東華書局 .
49. 50. Example
50. 51. Binary Arithmetic on Numeric Data <ul><li>Binary addition </li></ul><ul><li>Binary subtraction </li></ul><ul><li>Binary multiplication </li></ul><ul><li>Binary division </li></ul><ul><li>To be discussed in Unit 5 </li></ul>
51. 52. Part II: Alphanumeric Data <ul><li>Alphanumeric data: character, letter, symbol, digit) </li></ul><ul><li>Not for calculation, but for representation some “meanings” </li></ul><ul><li>Coding </li></ul><ul><ul><li>ASCII(as-kee):America Standard Code for Information Interchange </li></ul></ul><ul><ul><li>EBCDIC(eb-ce-dick):Extended Binary Coded Decimal Interchange Code (used by IBM, UNIVAC mainframes) </li></ul></ul><ul><ul><li>BIG-5: for Chinese characters </li></ul></ul><ul><ul><li>Uni-code </li></ul></ul>
52. 53. ASCII Code <ul><li>7-bit for each character, 2 7 =128 combinations for 128 characters </li></ul>
53. 54. Extended ACSII 8-bit for each character, 2 8 =256 combinations for 256 characters 。
54. 55. Examples N: 4E=00101110 a: 61=01100001 t: 74=01110100 i: 69=01101001 o: 6F=01101111 n: 6E=01101110 a: 61=01100001 l: 6C=01101010
55. 56. EBCDIC <ul><li>8-bit for each character </li></ul><ul><ul><li>4-bit Zone bits: identifying the code is for character, (un)signed number, or symbols </li></ul></ul><ul><ul><li>4-bit Digit bits: for numbers 0~9 。 </li></ul></ul>http://www.natural-innovations.com/computing/asciiebcdic.html
56. 58. Big5 Code <ul><li>Used for Chinese characters </li></ul><ul><li>1 character = 2 bytes (16 bits) </li></ul><ul><li>Example </li></ul>
57. 60. Unicode <ul><li>ASCII was very simplistic, and so was extended by adding 'extended' sets by various manufacturers. Apart from being confusing this was still restricted to 256 characters. </li></ul><ul><li>Now computers are more widely established around the world the need to show other characters such as Japanese and Chinese languages along with various symbols became more important. </li></ul><ul><li>2 bytes (16-bit) for each unicode </li></ul>
58. 61. Unicode samples
59. 62. Binary Operations on Alphanumeric Data <ul><li>Binary addition? </li></ul><ul><li>Binary subtraction? </li></ul><ul><li>Binary multiplication? </li></ul><ul><li>Binary division? </li></ul><ul><li>To be discussed in Unit 5 </li></ul>
60. 63. Part III: Extensions <ul><li>Everything in the computer is stored as a pattern of bits </li></ul><ul><ul><li>Binary distinctions are easy for hardware to work with </li></ul></ul><ul><li>Numbers are stored as a pattern of bits </li></ul><ul><ul><li>Computers use the binary number system </li></ul></ul><ul><li>Characters are stored as a pattern of bits </li></ul><ul><ul><li>One byte (8 bits) can represent one of 256 characters </li></ul></ul><ul><li>So, is everything in the computer stored as a number? </li></ul><ul><ul><li>No it isn’t, it’s stored as a bit pattern </li></ul></ul><ul><ul><li>There are many ways to interpret a bit pattern </li></ul></ul>
61. 64. Extension: Image representation <ul><li>Nature creations are analog, not digital </li></ul><ul><li>Digitizing (sampling) </li></ul><ul><li>Resolution: 800*600, 1026*768 </li></ul>Original 7*7 digitizing 14*14 digitizing
62. 65. For Black/White Images Original 7*7 digitizing 14*14 digitizing 0000000 1111100 … 00000000000000 00000000000000 01111111110000 01111111111000 01111111111110 … 0000 0001 1111 00 … 0000 0000 0000 0000 0000 0000 0000 0111 1111 1100 0001 11111111100001111111111110… (01F0..) 2 (00000007FA1..) 2
63. 66. For Colored Images
64. 67. Extension: Sound Representation time AMP
65. 68. Extension: Sampling Max. AMP/16 t 1 t 2 t 3 t 4 … 16=2 4 t 1 : 8=(1000) 2 t 2 : 9=(1001) 2 t 3 : 7=(0111) 2 t 4 : 2=(0010) 2 t 1 : 2=(0010) 2 … Quality vs sampling rate
66. 69. Conclusion