King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Lecture 5 – Data Representation 1 © 2005 Dr. Abdelhafid Bouhraoua COE 205 Lecture 5: Data Representation
King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Lecture 5 – Data Representation 2 © 2005 Dr. Abdelhafid Bouhraoua COE 205 Data in Computers Computers understand binary numbers only: Numbers: –Represented in binary Integers:+33, -37, +267 Reals: E16, 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)
King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Lecture 5 – Data Representation 3 © 2005 Dr. Abdelhafid Bouhraoua COE 205 What need to be represented From CPU point of view:ONLY Binary To 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
King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Lecture 5 – Data Representation 4 © 2005 Dr. Abdelhafid Bouhraoua COE 205 Numbering Systems N = d w-1 B w-1 +…+ d i B i + d 1 B 1 +d 0 B 0 d i : digits;B: Base d i in [0, B-1] Numbering SystemBaseDigits Set Binary20, 1 Octal80, 1, 2, 3, 4, 5, 6, 7 Decimal100, 1, 2, 3, 4, 5, 6, 7, 8, 9 Hexadecimal160, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F (728) 10 = ( ) 2 = (1330) 8 = (2D8) d = b = 1330o = 2D8h
King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Lecture 5 – Data Representation 5 © 2005 Dr. Abdelhafid Bouhraoua COE 205 Number Conversion Binary - Hexadecimal Hexa- decimal Bin.Sym A 1011B 1100C 1101D 1110E 1111F Binary to Hexadecimal D6C Conversion Table Lookup Hexadecimal to Binary 5D1F Conversion Table Lookup Octal Bin.Sym Binary to Octal Octal to Binary 5361 Conv. Table Lookup Hex Octal: Go to Binary First
King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Lecture 5 – Data Representation 6 © 2005 Dr. Abdelhafid Bouhraoua COE 205 Number Conversion Decimal – Binary, Octal, Hex Binary, Octal, Hex Decimal: –Represent the number in base 10 and compute the operations: –110110b = 1x x x x x x2 0 = 54 –236o = 2x x x8 0 = 158 –3Ch = 3x Cx16 0 = 60 Decimal Binary, Octal, Hex –Remainder Set of Successive Division by Target Base (2, 8 or 16) N = Q 0 x 2 + R 0 First Division Q 0 = Q 1 x 2 + R 1 Second Division Q 1 = Q 2 x 2 + R 2 Third Division …. Q n-1 = 0 x 2 + R n Cannot divide anymore N = (((((…(R n x2) x 2 + R n-1 ) x 2 +.) x2 + …..x2 + R 1 ) x2 + R 0 N = R n x 2 n + R n-1 x 2 n-1 + ….. + R 1 x 2 + R 0
King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Lecture 5 – Data Representation 7 © 2005 Dr. Abdelhafid Bouhraoua COE 205 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
King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Lecture 5 – Data Representation 8 © 2005 Dr. Abdelhafid Bouhraoua COE 205 Number Representation Numbers in computers –Just a representation of real numbers –Real numbers have an infinite number of digits 153 ten = … … two = two in a 16 bits word Add, Subtract, Multiply, Divide Result bigger than number of available slots (bits) Overflow
King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Lecture 5 – Data Representation 9 © 2005 Dr. Abdelhafid Bouhraoua COE 205 Signed Numbers Subtraction of a big number from small number is a negative number Need for Signed Numbers Representation Three Main ways: –Sign + Magnitude –1’s Complement –2’s Complement
King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Lecture 5 – Data Representation 10 © 2005 Dr. Abdelhafid Bouhraoua COE 205 Sign and Magnitude Intuitive Representation: Sign + Magnitude SignMagnitude Range [-2 n-1 -1, +2 n-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
King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Lecture 5 – Data Representation 11 © 2005 Dr. Abdelhafid Bouhraoua COE 205 One’s Complement Negative numbers are bit-to-bit complements of positive numbers –+9 on 8 bits = –-9 on 8 bits = Range [-2 n-1 -1, +2 n-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: – – 0001
King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Lecture 5 – Data Representation 12 © 2005 Dr. Abdelhafid Bouhraoua COE 205 Two’s Complement Range [-2 n-1, +2 n-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)
King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department College of Computer Science And Engineering College of Computer Science And Engineering Lecture 5 – Data Representation 13 © 2005 Dr. Abdelhafid Bouhraoua COE 205 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