69
111/06/14 Copyright: [email protected]. tw 1 Unit 4: The Number Systems and Data Representation

Number Systems

Embed Size (px)

Citation preview

Page 1: Number  Systems

112/04/12 Copyright: [email protected] 1

Unit 4: The Number Systems and Data Representation

Page 2: Number  Systems

112/04/12 Copyright: [email protected] 2

Problem-solving using “computers”

• Computers solve “computable” problems

A ProblemA ProblemDescribing

The Problemin Math.

Describing The Problem

in Math.

“Computing”The

CorrespondingMath.

Problem

“Computing”The

CorrespondingMath.

Problem

ReturningThe Result

ReturningThe Result

Solution To The Problem

Solution To The Problem

Human problem-solving v.s. computer-based problem-solving

Page 3: Number  Systems

112/04/12 Copyright: [email protected] 3

Transformation

input

think

act

input

compute

act

Binary system

Page 4: Number  Systems

112/04/12 Copyright: [email protected] 4

The Number System

• A number system is to enumerate the “states” of something– For example, money, days, month, year,

minutes, hours, …

• In human’s world, we have very good tools

…which counts 10 states

Page 5: Number  Systems

112/04/12 Copyright: [email protected] 5

The Needs of Number Systems

• However, there are some states are not Decimal. For example,– Days and weeks– Minutes and hours– Months and year

• We need convenient representations for different “nature states”

Page 6: Number  Systems

112/04/12 Copyright: [email protected] 6

Page 7: Number  Systems

112/04/12 Copyright: [email protected] 7

Egyptian numerals (10-based)

Source: http://www-gap.dcs.st-and.ac.uk/~history

Page 8: Number  Systems

112/04/12 Copyright: [email protected] 8

Babylonians numerals (60-based)

Source: http://www-gap.dcs.st-and.ac.uk/~history

Page 9: Number  Systems

112/04/12 Copyright: [email protected] 9

1*603 + 57*602 + 46*60 + 40*600 = 424000

Source: http://www-gap.dcs.st-and.ac.uk/~history

Page 10: Number  Systems

112/04/12 Copyright: [email protected] 10

Chinese numerals (10-based)

4359

45698

Source: http://www-gap.dcs.st-and.ac.uk/~history

壹貳參肆伍陸柒捌玖拾佰仟萬億兆京…

Page 11: Number  Systems

112/04/12 Copyright: [email protected] 11

The Number Systems

• A number system specifies how/what “numbers” represent and operate.

• A K-based number system uses N different symbols to represent N different entities or “states”

• Each symbol is a “digit”

• Multiple digits are used for describing M>N states

Page 12: Number  Systems

112/04/12 Copyright: [email protected] 12

K-based Number System

• 2-based: ON/OFF; Yes/No; T/F; 1/0; etc.

• 3-based: A/B/C; True/False/Unknown; 0/1/2; etc.

• …

• 7-based: Mon/Tue/Wes/Thu/Fri/Sat/Sun; 0/1/2/3/4/5/6; etc.

• 8-based: ; 0/1/2/3/4/5/6/7

Page 13: Number  Systems

112/04/12 Copyright: [email protected] 13

K-based Number System

• 10-based: /////////; 0/1/2/3/4/5/6/7/8/9; etc.

• 16-based: ///////////; 0/1/2/3/4/5/6/7/8/9/A/B/C/D/E/F; etc.

State-1State-2

State-3State-4

State-5State-6

State-7State-8

State-9State-10

State-11State-12

State-13State-14

State-15State-16

Page 14: Number  Systems

112/04/12 Copyright: [email protected] 14

K-based Number SystemIn the 16-based number system:

0/1/2/3/4/5/6/7/8/9/A/B/C/D/E/F; etc.

Why not “10” 2 digits

Page 15: Number  Systems

112/04/12 Copyright: [email protected] 15

More States

In a 6-based number system : state 1 (empty state) (0) : state 2 (1st) : state 3 (2nd) : state 4 (3rd) : state 5 (4th) : state 6 (5th)

: state 7 (6th) : state 8 (7th) : state 9 (8th) : state 10 (9th) : state 11 (10th) : state 12 (13th)

• …

Page 16: Number  Systems

112/04/12 Copyright: [email protected] 16

More States

In a 6-based number system

• 0: state 1 (empty state) (0)

• 1: state 2 (1st)

• 2: state 3 (2nd)

• 3: state 4 (3rd)

• 4: state 5 (4th)

• 5: state 6 (5th)

• 10: state 7 (6th)

• 11: state 8 (7th)

• 12: state 9 (8th)

• 13: state 10 (9th)

• 14: state 11 (10th)

• 15: state 12 (13th)

• 20: state 13 (14th)

• …

Page 17: Number  Systems

112/04/12 Copyright: [email protected] 17

The Number of Cases

• Generally, in a K-based number system,– 1 digit describes K1 cases– 2 digits describes K2 cases– 3 digits describes K3 cases– N digits describes KN cases

• For example,– 2 3-based (0,1,2) digits: 00, 01, 02, 10, 11, 12, 20, 21,

22 (32=9 cases)– 3 10-based (0,1,2,..,9) digits: 000, 001, 002, 003, 004,

005, 006,007, 008, 009, 010, 011,…,998, 999 (103=1000 cases)

Page 18: Number  Systems

112/04/12 Copyright: [email protected] 18

K-based vs The Decimal system (10-based)

• In general, any K-based number N can be expressed as

• Usually, Nk is denoted as – (Ap-1Ap-2….A1A0.A-1A-2….A-q)k – Ap-1:Most Significant Digit, MSD– A-q :Least Significant Digit, LSD

Page 19: Number  Systems

112/04/12 Copyright: [email protected] 19Source: 計算機概論 , 王孝熙著 , 東華書局 .

Page 20: Number  Systems

112/04/12 Copyright: [email protected] 20

Using octal and hex numbers

• Computers use binary, but the numbers are too long and confusing for people

• Translation between binary and octal or hex is easy

– One octal digit equals three binary digits 101101011100101000001011 5 5 3 4 5 0 1 3– One hexadecimal digit equals four binary digits 101101011100101000001011 B 5 C A 0 B

Page 21: Number  Systems

112/04/12 Copyright: [email protected] 21Source: 計算機概論 , 王孝熙著 , 東華書局 .

Page 22: Number  Systems

112/04/12 Copyright: [email protected] 22Source: 計算機概論 , 王孝熙著 , 東華書局 .

Page 23: Number  Systems

112/04/12 Copyright: [email protected] 23Source: 計算機概論 , 王孝熙著 , 東華書局 .

Page 24: Number  Systems

112/04/12 Copyright: [email protected] 24Source: 計算機概論 , 王孝熙著 , 東華書局 .

Page 25: Number  Systems

112/04/12 Copyright: [email protected] 25Source: 計算機概論 , 王孝熙著 , 東華書局 .

Page 26: Number  Systems

112/04/12 Copyright: [email protected] 26Source: 計算機概論 , 王孝熙著 , 東華書局 .

Page 27: Number  Systems

112/04/12 Copyright: [email protected] 27

The binary number system

• In modem computers, the binary number system is easier to be implemented.

• Currently, they are implemented in silicon chips (VLSI)– Circuits for computation

01111

0001110010

15

3+10

+2 18

Page 28: Number  Systems

112/04/12 Copyright: [email protected] 28

The binary number system

• The binary (base 2 or 2-based) number system uses two “binary digits, ” (abbreviation: bits) -- 0 and 1

• A bit is a single two-valued quantity: yes or no, true or false, on or off, high or low, good or bad

• One bit can distinguish between two cases: T, F (True/False; Yes/No)

• Two bits can distinguish between four cases: TT, TF, FT, FF• Three bits can distinguish between eight cases: TTT, TTF,

TFT, TFF, FTT, FTF, FFT, FFF• In general, n bits can distinguish between 2n cases• A byte is 8 bits, therefore 28 = 256 cases

Page 29: Number  Systems

112/04/12 Copyright: [email protected] 29

Data Processing in Computers with the Binary System

• Data processing in binary computers– Numeric data: +2, -5.5, 50000.235698, etc.– Alphanumeric data: characters, name, address,

telephone number, etc.

• Two main tasks: – All data are converted into proper binary

formats (bits)– Rules for counting and computing these bits

Page 30: Number  Systems

112/04/12 Copyright: [email protected] 30

Part I: Numeric Data• Numeric data:

– integers, real – positive, negative

• Representation of numeric data– Sign-magnitude– 1’s Complement – 2’s Complement

• Negative/Positive in a n-bit integer I

I=(An-1An-2….A1A0)2

– An-1= “0”, I is positive– An-1=“1”, I is negative

Page 31: Number  Systems

112/04/12 Copyright: [email protected] 31

Sign-Magnitude• An n-bit integer I=

– Min: 00000…0 (n-1 0’s)=0– Max: 11111…1 (n-1 1’s)=2n-1-1– +0= (0000)2 vs –0=(1000)2

– +3= (0011)2 vs –3=(1011)2

• Disadvantages– 2 different 0’s (+0/-0 )– Not easy to be implemented in simple circuits (usually ad

der)

(An-1An-2….A1A0)2

sign:0(+), 1(-) magnitude

Page 32: Number  Systems

112/04/12 Copyright: [email protected] 32

1’s Complement• An n-bit integer I=

– For positive integers: the same as that in sign-magnitude

– For negative integers: complement their positive representations

– For example: 4-bit integers: • +3=(0011)2, -3=(1100)2

• +0=(0000)2, -0=(1111)2

• Disadvantages– 2 different 0’s (+0/-0)– Easy to be implemented in simple circuits (usually adder) but not

as efficient as 2’s complement

(An-1An-2….A1A0)2

sign:0(+), 1(-) magnitude

Page 33: Number  Systems

112/04/12 Copyright: [email protected] 33

2’s Complement• An n-bit integer I=

– For positive integers: the same as that in sign-magnitude

– For negative integers: find their 1’s complement + 1• For example: 4-bit integers:

• +3 = (0011)2 , -3 = (1100)2+1 = (1101)2

• +0 = (0000)2, -0 = (1111)2 + 1 = (0000)2

• There is only one “0”

(An-1An-2….A1A0)2

sign:0(+), 1(-) magnitude

Page 34: Number  Systems

112/04/12 Copyright: [email protected] 34

Comparison: 4-bit representation of integers

Page 35: Number  Systems

112/04/12 Copyright: [email protected] 35

Range of integersSuppose that we use a byte (8-bit) to represent an integer

Max. (+) Max. (-) +0 -0

Sign-Magnitude

(01111111)2=+127 (11111111) 2=-127 (00000000) 2 (10000000) 2

1‘s Complement

(01111111)2=+127 (10000000) 2=-127 (00000000) 2 (11111111) 2

2‘s complement

(01111111) 2=+127 (10000000) 2=-128 (00000000) 2 (00000000) 2

Positive numbers Negative numbers

Sign-Magnitude 0~2n-1-1 -(2n-1-1)~01‘s Complement 0~2n-1-1 -(2n-1-1)~02‘s complement 0~2n-1-1 -(2n-1)~0

Page 36: Number  Systems

112/04/12 Copyright: [email protected] 36

Note

• Representation level

• Implementation level

Page 37: Number  Systems

112/04/12 Copyright: [email protected] 37

Other Binary Codes for Decimal Numbers

• BCD code

• 2421 code

• Excess-3 code

• 84-2-1 code

• 4-bit for a number (0~9)

Page 38: Number  Systems

112/04/12 Copyright: [email protected] 38

8421 BCD Code• In the 8421 Binary Coded Decimal (BCD) representation

each decimal digit is converted to its 4-bit pure binary equivalent.

• Each decimal number maps to four bits and is weighted by its bit-position (each bit represents a number, 1, 2, 4, 8)

• BCD code is also called as 8421 code

Page 39: Number  Systems

112/04/12 Copyright: [email protected] 39

4221 BCD Code

• In 4221 BCD code, each bit is weighted by 4, 2, 2 and 1 respectively.

• Unlike BCD coding there are no invalid representations.

Decimal 42211's

complement0 0000 11111 0001 11102 0010 11013 0011 11004 1000 01115 0111 10006 1100 00117 1101 00108 1110 00019 1111 0000

advantages

Page 40: Number  Systems

112/04/12 Copyright: [email protected] 40

Excess-3 Code• Add 3 for each binary number

• Examples– Excess-3 code of “210” = (0010)2 +(0011)2

=(0101)2

– Excess-3 code of “510” = (0101)2+(0011)2=(1000)2

• Advantages

Page 41: Number  Systems

112/04/12 Copyright: [email protected] 41

84-2-1 Code• 4-bit for each number (0~9) weighted (left to righ

t) as 8, 4, -2, and -1 。• Example

– 84-2-1 code of “3” = 0101 (0+4+0+(-1)=3)

– 84-2-1 code of “5” =1011 (8+0+(-2)+(-1)=5)

Page 42: Number  Systems

112/04/12 Copyright: [email protected] 42

Self-complementing code

Source: 計算機概論 , 王孝熙著 , 東華書局 .

Page 43: Number  Systems

112/04/12 Copyright: [email protected] 43

Gray Codes

• 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.

• In gray coding, only one bit changes between subsequent numbers.

• Generating gray codes: start with all 0s and then proceed by changing the least significant bit (LSB) which will bring about a new state.

• Advantages: fast, relatively free from errors.

Page 44: Number  Systems

112/04/12 Copyright: [email protected] 44

Gray Codes

• Gray code is not unique, there are many possibilities to generate gray codes

• For example, – G1={0=00, 1=01, 2=11, 3=10}

– G2={00=10, 1=11, 2=01, 3=00}

• Unique gray code: reflected Gray code

Page 45: Number  Systems

112/04/12 Copyright: [email protected] 45

從十進位 → 反射葛雷碼

Source: 計算機概論 , 王孝熙著 , 東華書局 .

Page 46: Number  Systems

112/04/12 Copyright: [email protected] 46

從反射葛雷碼 → 十進位數字

Source: 計算機概論 , 王孝熙著 , 東華書局 .

Page 47: Number  Systems

112/04/12 Copyright: [email protected] 47

Floating-Point Representation

• Integers are fixed-point numbers in binary computers• Floating-point literals are written with a decimal point:

8.5 -7.923 5.000 • Real numbers are represented as “floating-point” numbers

in binary system (all computers)• The representation of floating-point numbers are various in

different CPU• In Intel 80486 CPU

– Single Precision: 32 bits– Double Precision: 64 bits– Extended Precision: 80bits

Page 48: Number  Systems

112/04/12 Copyright: [email protected] 48

Floating-point literals

• Floating-point numbers may also be written in “scientific notation”– times a power of 10

• We use E to represent “times 10 to the”• Example: 4.32E5 means 4.32 x 105

http://www.nuvisionmiami.com/books/asm/workbook/floating_tut.htm

Page 49: Number  Systems

112/04/12 Copyright: [email protected] 49

• 範例

Source: 計算機概論 , 王孝熙著 , 東華書局 .

Page 50: Number  Systems

112/04/12 Copyright: [email protected] 50

Example

Binary ValueBiased

Exponent Sign, Exponent, Mantissa

 - 1.11 127 1  01111111  11000000000000000000000

 +1101.101 130 0  10000010  10110100000000000000000

 - .00101 124 1  01111100  01000000000000000000000

 +100111.0 132 0  10000100  00111000000000000000000

 +.0000001101011 120 0  01111000  10101100000000000000000

Page 51: Number  Systems

112/04/12 Copyright: [email protected] 51

Binary Arithmetic on Numeric Data

• Binary addition

• Binary subtraction

• Binary multiplication

• Binary division

To be discussed in Unit 5

Page 52: Number  Systems

112/04/12 Copyright: [email protected] 52

Part II: Alphanumeric Data• Alphanumeric data: character, letter, symbol, digit)• Not for calculation, but for representation some “mea

nings”• Coding

– ASCII(as-kee):America Standard Code for Information Interchange

– EBCDIC(eb-ce-dick):Extended Binary Coded Decimal Interchange Code (used by IBM, UNIVAC mainframes)

– BIG-5: for Chinese characters

– Uni-code

Page 53: Number  Systems

112/04/12 Copyright: [email protected] 53

ASCII Code• 7-bit for each character, 27=128 combinations for 128 characters

Page 54: Number  Systems

112/04/12 Copyright: [email protected] 54

Extended ACSII8-bit for each character, 28=256 combinations for 256 characters 。

Page 55: Number  Systems

112/04/12 Copyright: [email protected] 55

Examples

N: 4E=00101110 a: 61=01100001t: 74=01110100i: 69=01101001o: 6F=01101111 n: 6E=01101110a: 61=01100001l: 6C=01101010

Page 56: Number  Systems

112/04/12 Copyright: [email protected] 56

EBCDIC

• 8-bit for each character – 4-bit Zone bits: identifying the code is for char

acter, (un)signed number, or symbols– 4-bit Digit bits: for numbers 0~9 。

http://www.natural-innovations.com/computing/asciiebcdic.html

Page 57: Number  Systems

112/04/12 Copyright: [email protected] 57

Page 58: Number  Systems

112/04/12 Copyright: [email protected] 58

Big5 Code

• Used for Chinese characters

• 1 character = 2 bytes (16 bits)

• Example

Page 59: Number  Systems

112/04/12 Copyright: [email protected] 59

Page 60: Number  Systems

112/04/12 Copyright: [email protected] 60

Unicode

• 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.

• 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.

• 2 bytes (16-bit) for each unicode

Page 61: Number  Systems

112/04/12 Copyright: [email protected] 61

Unicode samples

Page 62: Number  Systems

112/04/12 Copyright: [email protected] 62

Binary Operations on Alphanumeric Data

• Binary addition?

• Binary subtraction?

• Binary multiplication?

• Binary division?

To be discussed in Unit 5

Page 63: Number  Systems

112/04/12 Copyright: [email protected] 63

Part III: Extensions

• Everything in the computer is stored as a pattern of bits– Binary distinctions are easy for hardware to work with

• Numbers are stored as a pattern of bits– Computers use the binary number system

• Characters are stored as a pattern of bits– One byte (8 bits) can represent one of 256 characters

• So, is everything in the computer stored as a number?– No it isn’t, it’s stored as a bit pattern

– There are many ways to interpret a bit pattern

Page 64: Number  Systems

112/04/12 Copyright: [email protected] 64

Extension: Image representation

• Nature creations are analog, not digital• Digitizing (sampling)• Resolution: 800*600, 1026*768

Original 7*7 digitizing 14*14 digitizing

Page 65: Number  Systems

112/04/12 Copyright: [email protected] 65

For Black/White Images

Original 7*7 digitizing 14*14 digitizing

00000001111100…

0000000000000000000000000000011111111100000111111111100001111111111110…

00000001111100…

0000000000000000000000000000011111111100000111111111100001111111111110…

(01F0..)2

(00000007FA1..)2

Page 66: Number  Systems

112/04/12 Copyright: [email protected] 66

For Colored Images

Page 67: Number  Systems

112/04/12 Copyright: [email protected] 67

Extension: Sound Representation

time

AMP

Page 68: Number  Systems

112/04/12 Copyright: [email protected] 68

Extension: Sampling

Max. AMP/16

t1 t2 t3 t4 …

16=24

t1: 8=(1000)2

t2: 9=(1001)2

t3: 7=(0111)2

t4: 2=(0010)2

t1: 2=(0010)2

Quality vs sampling rate

Page 69: Number  Systems

112/04/12 Copyright: [email protected] 69

Conclusion