coe 205

13
COE 205 King Fahd University of Petroleum and Minerals Computer Engineering Department College of Computer Science And Engineering Lecture 5 – Data Representation 1 © 2005 Dr. Abdelhafid Bouhraoua COE 205 Lecture 5: Data Representation

Upload: tosca

Post on 23-Jan-2016

54 views

Category:

Documents


0 download

DESCRIPTION

COE 205. Lecture 5: Data Representation. Data in Computers. Computers understand binary numbers only: Numbers: Represented in binary Integers:+33, -37, +267 Reals:-123.45 E16, -59.6 E-12 Characters: Alphanumeric: A, c, Ctrl, Shift Represented by numeric codes Multimedia - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: COE 205

COE 205COE 205

King Fahd University of Petroleum and MineralsKing Fahd University of Petroleum and Minerals

Computer Engineering Department

Computer Engineering Department

College of ComputerScience And Engineering

College of ComputerScience And Engineering

Lecture 5 – Data Representation 1© 2005 Dr. Abdelhafid Bouhraoua

COE 205

Lecture 5:

Data Representation

Page 2: COE 205

COE 205COE 205

King Fahd University of Petroleum and MineralsKing Fahd University of Petroleum and Minerals

Computer Engineering Department

Computer Engineering Department

College of ComputerScience And Engineering

College of ComputerScience And Engineering

Lecture 5 – Data Representation 2© 2005 Dr. Abdelhafid Bouhraoua

Data in Computers• Computers understand binary numbers only:• Numbers:

– Represented in binary• Integers: +33, -37, +267• Reals: -123.45 E16, -59.6 E-12

• Characters: Alphanumeric: A, c, Ctrl, Shift– Represented by numeric codes

• Multimedia– Still Images: Many formats, all numeric. Based on numeric representation of the

RGB components of each point of the image (pixel)– Video Images: Many formats also. Based on same principal as still images but

applied on moving portions of the image only.– Sounds: Many formats. All numeric. Based on numeric representation of the

amplitude of the sound wave over time.• Records and Database Elements:

– Combination of Alphanumeric strings and numbers• Scientific Data

– Combination of Numbers, Records, Multimedia and Statistical Data (numbers)

Page 3: COE 205

COE 205COE 205

King Fahd University of Petroleum and MineralsKing Fahd University of Petroleum and Minerals

Computer Engineering Department

Computer Engineering Department

College of ComputerScience And Engineering

College of ComputerScience And Engineering

Lecture 5 – Data Representation 3© 2005 Dr. Abdelhafid Bouhraoua

What need to be represented

From CPU point of view: ONLY BinaryTo Standardize: At CPU Level Need to represent:• Numbers• Characters• No need to represent other items

– No need to represent Images at CPU level– No need to represent Sounds at CPU level– No need to represent records at CPU level– Taken care by HL language constructs– Goal: Machine independent representation: STANDARD

Represent ONLY Numbers and Characters

Page 4: COE 205

COE 205COE 205

King Fahd University of Petroleum and MineralsKing Fahd University of Petroleum and Minerals

Computer Engineering Department

Computer Engineering Department

College of ComputerScience And Engineering

College of ComputerScience And Engineering

Lecture 5 – Data Representation 4© 2005 Dr. Abdelhafid Bouhraoua

Numbering SystemsN = dw-1Bw-1 +…+ diBi + d1B1 +d0B0

di: digits; B: Basedi in [0, B-1]

Numbering System Base Digits Set

Binary 2 0, 1

Octal 8 0, 1, 2, 3, 4, 5, 6, 7

Decimal 10 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Hexadecimal 16 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F

(728)10 = (1011011000)2 = (1330)8 = (2D8)16

728d = 1011011000b = 1330o = 2D8h

Page 5: COE 205

COE 205COE 205

King Fahd University of Petroleum and MineralsKing Fahd University of Petroleum and Minerals

Computer Engineering Department

Computer Engineering Department

College of ComputerScience And Engineering

College of ComputerScience And Engineering

Lecture 5 – Data Representation 5© 2005 Dr. Abdelhafid Bouhraoua

Number ConversionBinary - HexadecimalHexa-

decimal

Bin. Sym.

0000 0

0001 1

0010 2

0011 3

0100 4

0101 5

0110 6

0111 7

1000 8

1001 9

1010 A

1011 B

1100 C

1101 D

1110 E

1111 F

Binary to Hexadecimal000100110100110101101100

0

134D6C

Conversion Table Lookup

Hexadecimal to Binary5D1F8

0101 1101 0001 1111 1000

Conversion Table Lookup

Octal

Bin. Sym.

000 0

001 1

010 2

011 3

100 4

101 5

110 6

111 7

Binary to Octal001110101101100

0 0

16554

Octal to Binary5361

Conv. Table Lookup

Conv. Table Lookup

101 011 110 001

Hex Octal: Go to Binary First

Page 6: COE 205

COE 205COE 205

King Fahd University of Petroleum and MineralsKing Fahd University of Petroleum and Minerals

Computer Engineering Department

Computer Engineering Department

College of ComputerScience And Engineering

College of ComputerScience And Engineering

Lecture 5 – Data Representation 6© 2005 Dr. Abdelhafid Bouhraoua

Number ConversionDecimal – Binary, Octal, Hex

• Binary, Octal, Hex Decimal:– Represent the number in base 10 and compute the operations:– 110110b = 1x25 + 1x24 + 0x23 + 1x22 + 1x21 + 0x20 = 54– 236o = 2x82 + 3x81 + 6x80 = 158– 3Ch = 3x161 + Cx160 = 60

• Decimal Binary, Octal, Hex– Remainder Set of Successive Division by Target Base (2, 8 or 16)

N = Q0 x 2 + R0 First DivisionQ0 = Q1 x 2 + R1 Second DivisionQ1 = Q2 x 2 + R2 Third Division….Qn-1 = 0 x 2 + Rn Cannot divide anymoreN = (((((…(Rn x2) x 2 + Rn-1) x 2 + .) x2 + …..x2 + R1) x2 + R0

N = Rn x 2n + Rn-1 x 2n-1 + ….. + R1 x 2 + R0

Page 7: COE 205

COE 205COE 205

King Fahd University of Petroleum and MineralsKing Fahd University of Petroleum and Minerals

Computer Engineering Department

Computer Engineering Department

College of ComputerScience And Engineering

College of ComputerScience And Engineering

Lecture 5 – Data Representation 7© 2005 Dr. Abdelhafid Bouhraoua

ACSII or Decimal vs Binary Numbers

• Attractive because: Human- Friendly representation• Non attractive to computers:

– Understand only binary

– Binary operations easy to perform

– Does not know how to perform operations on ASCII or Decimal number.

– Need to convert ASCII/Decimal to binary before and after operation

• Used only for I/O (User Interface)

Use Binary Representation

For Internal Computations

Convert to/from ASCII for I/O

Only

Page 8: COE 205

COE 205COE 205

King Fahd University of Petroleum and MineralsKing Fahd University of Petroleum and Minerals

Computer Engineering Department

Computer Engineering Department

College of ComputerScience And Engineering

College of ComputerScience And Engineering

Lecture 5 – Data Representation 8© 2005 Dr. Abdelhafid Bouhraoua

Number Representation• Numbers in computers

– Just a representation of real numbers

– Real numbers have an infinite number of digits

153ten = …..00000…0000010011001two

= 0000 0000 1001 1001two in a 16 bits word

Add, Subtract, Multiply, Divide

Result bigger than number of

available slots (bits)

Overflow

Page 9: COE 205

COE 205COE 205

King Fahd University of Petroleum and MineralsKing Fahd University of Petroleum and Minerals

Computer Engineering Department

Computer Engineering Department

College of ComputerScience And Engineering

College of ComputerScience And Engineering

Lecture 5 – Data Representation 9© 2005 Dr. Abdelhafid Bouhraoua

Signed Numbers• Subtraction of a big number from small

number is a negative numberNeed for Signed Numbers Representation

• Three Main ways:– Sign + Magnitude– 1’s Complement– 2’s Complement

Page 10: COE 205

COE 205COE 205

King Fahd University of Petroleum and MineralsKing Fahd University of Petroleum and Minerals

Computer Engineering Department

Computer Engineering Department

College of ComputerScience And Engineering

College of ComputerScience And Engineering

Lecture 5 – Data Representation 10© 2005 Dr. Abdelhafid Bouhraoua

Sign and Magnitude

Intuitive Representation: Sign + Magnitude

Sign Magnitude• Range [-2n-1 -1 , +2n-1-1]

– 8 bits: [-127, +127]

– 12 bits: [-2047, +2047]

• Advantages: Straight Forward• Disadvantages

– Has the problem of double representing the 0 (–0 and +0),

– Complicates the design of the logic circuits that handle signed-numbers arithmetic

Page 11: COE 205

COE 205COE 205

King Fahd University of Petroleum and MineralsKing Fahd University of Petroleum and Minerals

Computer Engineering Department

Computer Engineering Department

College of ComputerScience And Engineering

College of ComputerScience And Engineering

Lecture 5 – Data Representation 11© 2005 Dr. Abdelhafid Bouhraoua

One’s Complement• Negative numbers are bit-to-bit complements of

positive numbers– +9 on 8 bits = 00001001– -9 on 8 bits = 11110110

• Range [-2n-1 -1 , +2n-1-1]– 8 bits: [-127, +127]– 12 bits: [-2047, +2047]

• Advantages– Easy Representation– Easier arithmetic operations (add/sub) than sign + magnitude

• Disadvantages– Has the problem of double representing the 0 (–0 and +0), – Add/Sub still relatively complex

Examples: 0111 + 01110001 – 01110111 – 0001

Page 12: COE 205

COE 205COE 205

King Fahd University of Petroleum and MineralsKing Fahd University of Petroleum and Minerals

Computer Engineering Department

Computer Engineering Department

College of ComputerScience And Engineering

College of ComputerScience And Engineering

Lecture 5 – Data Representation 12© 2005 Dr. Abdelhafid Bouhraoua

Two’s Complement

• Range [-2n-1, +2n-1-1]– 8 bits: [-128, +127]– 12 bits: [-2048, +2047]

• Advantages– No double representation of 0 (the 2's complement of 0 is still 0), – Simplest Add/Subtract circuit design,– Add/Subtract operations is done in one-step, – The end result is already represented in 2's complement (not when overflow).

• Negative number represented as bit to bit complement of positive number plus 1

• Coming from: x + (-x) = 0 • Given x + x = -1 (check for any number)

Page 13: COE 205

COE 205COE 205

King Fahd University of Petroleum and MineralsKing Fahd University of Petroleum and Minerals

Computer Engineering Department

Computer Engineering Department

College of ComputerScience And Engineering

College of ComputerScience And Engineering

Lecture 5 – Data Representation 13© 2005 Dr. Abdelhafid Bouhraoua

Character Representation• ASCII

– Broadly Used– Standard– Limited in representing other languages– 7 bits + 1 bit (parity)

• Even Parity: put 1 in PB if # of 1s is even• Odd Parity: put 1 in PB if # of 1s is odd

• Unicode– 16 bits– Used to represent chars of all languages