Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Arithmetic and Data Representation http://www.pds.ewi.tudelft.nl/~iosup/Courses/2011_ti1400_3.ppt 1 TI1400/11-PDS TU-Delft Lecture 1 Making functions nand gates A Y ADD B A,B Y time 2 TI1400/11-PDS TU-Delft Lecture 1 Making functions Circuit Diagram http://xkcd.com/730/ TI1400/11-PDS TU-Delft Lecture 2 Programmable device 2,1 input stream Programmable Device READ(X) READ(Y) ADD(X,Y,Z) WRITE(Z) 3 output stream program • READ(X) means read next input value from input stream and store it internally as variable X • WRITE(X) means put value in variable X on output stream • ADD(X,Y,Z) means assign value of X+Y to Z TI1400/11-PDS TU-Delft Lecture 2 Von Neumann Architecture Memory X: 1 Y: 2 Z: 3 • • READ(X) READ(Y) ADD(X,Y,Z) WRITE(Z) TI1400/11-PDS Central Processing Unit TEMP_A: TEMP_B: RESULT: IR: arithmetic unit Input CONTROL Output PC: TU-Delft Problem: How to Represent and Use Data? 1. Representation 2. Arithmetic 3. Conversion TI1400/11-PDS TU-Delft The Y2K Problem/The Millenium Bug (2000) Representation (Gone Wrong) • Abbreviating the year as two-digits instead of four - 99 instead of 1999 00 instead of 2000 Cost saving (space is money) How to represent 2096? 98 < 99, but 99 < 00?! • Problem identified by Spencer Bolles in 1985 - Y2K acronym used by David Eddy in 1995 • $150B in US (IDC estimate) TI1400/11-PDS TU-Delft The FDIV Problem (1993) Arithmetic (Gone Wrong) • 4195835.0/3145727.0 = 1.333 820 449 136 241 002 5 4195835.0/3145727.0 = 1.333 739 068 902 037 589 4 • Pentium used the radix-4 SRT algorithm (which is fast) • “The [problem] can be traced to five missing entries in a lookup table” • Solution: recall defective processors ($350M) or scale operands and use 80-bit extended-precision internal Pentium format (formats are discussed in this lecture) • … oh, and hardware errors still happen (latest, Intel’s Cougar/ Sandy Chipsets in February 1, 2011—est. cost $700M) TI1400/11-PDS TU-Delft Ariane 5 (1996) Conversion (Gone Wrong) June 4, 1996: Ariane 501 Satellite Launch explodes 40s into the flight sequence Cost: $370M “The internal SRI* software exception was caused during execution of a data conversion from 64-bit floating point to 16bit signed integer value. The floating point number which was converted had a value greater than what could be represented by a 16-bit signed integer.” Inquiry Board Report 9 TI1400/11-PDS TU-Delft Data Type Representation 1. 2. 3. 4. 5. 6. Integer numbers Real numbers Booleans Characters Composite types (e.g. arrays) Objects 14 TI1400/11-PDS TU-Delft Number systems 0 1 10110 66 277 -15 0100 1110 15 TI1400/11-PDS TU-Delft Question • Which decimal number is associated with the bitstring <110> ? 16 TI1400/11-PDS TU-Delft Definition and Representation Integers (Z) = Set of natural numbers including 0 and their non-zero negatives Integer numbers are generally represented by: • An alfabet • A string X of n elements from : Xn n • Applying Xn to a valuation function F - signature: F : n Z 17 TI1400/11-PDS TU-Delft Examples R = {I, V, M, ....} • - VI = 6 IV = 4 • - • 10 = {0, 1, 2, 3, .....,9} 27710 = 2*102 + 7*101 + 7*100 radix Many sets and valuation functions F possible 18 TI1400/11-PDS TU-Delft Question What value has 267 ? 19 TI1400/11-PDS TU-Delft Binary representation • In computer, we have 2 ={0, 1} • There are, however, many valuation functions F 1110 F1 66 277 -15 F2 F3 0011 1010 20 TI1400/11-PDS TU-Delft Criteria for representations • • • • Let Xn = < xn-1, xn-2 , ....., x0 > Range of natural numbers of X Representation of 010 X Efficiency of implementation - Sign inversion : A -A - Addition: A+B - Extension: n bit m bit representation, m>n 21 TI1400/11-PDS TU-Delft Data Type Representation 1. 2. 3. 4. 5. 6. Integer numbers 1.1. Sign-Magnitude 1.2. Two’s Complement (2C) Real numbers 1.3. One’s Complement (1C) Booleans 1.4. Excess-X Characters Composite types (e.g. arrays) Objects 22 TI1400/11-PDS TU-Delft 1. Integer Representation 1.1. Sign Magnitude • F: sign(xn-1){xn-22n-2 + ....+ x020} sign(1) = - and sign(0)= + • Range: [-(2n-1 -1), 2n-1 -1] • Zero: 100... and 000... • Inversion: change only sign bit X sign bit 1 1 0 V = - 21 = -2 0 1 0 V = +21 = 2 23 TI1400/11-PDS TU-Delft 1. Integer Representation 1.2. Two’s complement (2C) • F: -xn-12n-1 + xn-22n-2 + ....+ x020 • Range: [-2n-1, 2n-1-1] • Zero: 000... (only a single representation) X sign 1 1 0 V = - 22 +21 = -2 0 1 0 V = +21 = 2 24 TI1400/11-PDS TU-Delft 1. Integer Representation 1.2. 2C 2C negative 100 101 110 111 000 001 010 011 -4 -3 -2 -1 0 1 2 3 Inversion rule: - invert all bits - add 1 - General: A + Å = 2n 110 001 1 + 010 25 TI1400/11-PDS TU-Delft 1. Integer Representation 1.3. One’s Complement (1C) • F: -xn-1(2n-1-1) + xn-22n-2 + ....+ x020 • Range: [-(2n-1-1), 2n-1-1] • Zero: 111... and 000... X sign 1 1 0 V = - 22 +1+21 = -1 0 1 0 V = +21 = 2 26 TI1400/11-PDS TU-Delft 1. Integer Representation 1.3. 1C 1C negative 100 101 110 111 000 001 010 011 -3 -2 -1 0 0 1 2 3 110 Inversion rule: - invert all bits - General: A + Å = 2n-1 001 27 TI1400/11-PDS TU-Delft 1. Integer Representation 1.4. Excess-x • F: xn-12n-1 + xn-22n-2 + ....+ x020 - 2n-1 • Range: [-2n-1, 2n-1-1] • Zero: 10000..... X 1 1 0 V = 22 + 2 1 - 2 2 = 2 0 1 0 V = +21 - 22= -2 28 TI1400/11-PDS TU-Delft Data Type Representation 1. 2. 3. 4. 5. 6. Integer numbers 1.5. Addition 1.6. Sign Extension for 2C Real numbers 1.7. Other Representations Booleans 1.8. Conversion Characters Composite types (e.g. arrays) Objects 29 TI1400/11-PDS TU-Delft 1. Integer Representation 1.5. Addition • SM - Sign and Magnitude are processed separately - Opposite sign operands: subtraction • 2C - All bits can be added, regardless of position • 1C - All bits can be added, regardless of position - When carry at position 2n , add 1 to result 30 TI1400/11-PDS TU-Delft 1. Integer Representation 1.5. Addition Example X 000 001 010 011 100 101 110 111 2C 0 1 2 3 -4 -3 -2 -1 1C 0 1 2 3 -3 -2 -1 0 SM Ex-4 0 -4 1 -3 2 -2 3 -1 0 0 -1 1 -2 2 -3 3 31 TI1400/11-PDS TU-Delft 1. Integer Representation 1.6. Sign Extension (for 2C) • Represent n-bit in k-bit number • If k>n, extend sign bit to the left • If k<n, - n-k+1 bits equal, remove lefmost n-k bits - if not, overflow • Example: n=6, k=4 4 = 000100 -> 0100 9 = 001001 -> overflow -6 = 111010 -> 1010 32 TI1400/11-PDS TU-Delft 1. Integer Representation 1.7. Other Representations Binary Coded Decimal decimal X 0 0000 1 0001 2 0010 3 0011 4 0100 5 0101 6 0110 7 0111 8 1000 9 1001 33 TI1400/11-PDS TU-Delft 1. Integer Representation 1.7. Other Representations Radix 2, 8, 10, 16 binair octal dec. hex 0 0 0 0 1 1 1 1 10 2 2 2 11 3 3 3 100 4 4 4 101 5 5 5 110 6 6 6 111 7 7 7 1000 10 8 8 1001 11 9 9 1010 12 10 A 1011 13 11 B 1100 14 12 C 1101 15 13 D 1110 16 14 E 1111 17 15 F L33T-Speak h4x0r hacker 1007 money (loot) 1337 elite (leet) http://www.jayssite.com/stuff/l33t/l33t_translator.html 34 TI1400/11-PDS TU-Delft 1. Integer Representation 1.8. Conversion [1/2] • Simple, if one radix is power of other radix - binary to octal 10100101001 = 010 100 101 001 = 2451oct - binary to hexadecimal 10100101001 = 0101 1010 1001 = 5A9hex = 0x05A9 35 TI1400/11-PDS TU-Delft 1. Integer Representation 1.8. Conversion [2/2] • From binary to decimal 10110101001 = 210 + 28 + 27 + 25+ 23+ 20 = 1449 • From decimal to binary (first method) 1449 = 210 +425 = 210 +28 +169 =..= 10110101001 • From decimal to binary (second method) 1449 = 2*724 +1 LSB 724 = 2*362 +0 ...... =...... 1 = 0*1 +1 36 TI1400/11-PDS TU-Delft Data Type Representation 1. 2. 3. 4. 5. 6. Integer numbers 1.9. Binary Addition Real numbers 1.10. Operation Overflow Booleans Characters Composite types (e.g. arrays) Objects 37 TI1400/11-PDS TU-Delft 1. Integer Representation 1.9. Binary Addition Addition of binary digits • Addition of two bits 0 0 + 0 1 0 + 1 0 1 + 1 1 x 1 y + + 10 cs • Sum s and Carry c 38 TI1400/11-PDS TU-Delft 1. Integer Representation 1.9. Binary Addition More positions xi 0 0 1 yi 0 1 1 ci 0 1 1 si 0 0 1 ci+1 0 1 1 . . ci+1 . xi yi ci si . . . . truth table (partial) 39 TI1400/11-PDS TU-Delft 1. Integer Representation 1.9. Binary Addition Basic Adder xi ci+1 yi Full Adder ci si 40 TI1400/11-PDS TU-Delft 1. Integer Representation 1.9. Binary Addition Ripple Carry Adder FA FA FA 41 TI1400/11-PDS TU-Delft 1. Integer Representation 1.10. Overflow Addition/subtraction • 2C - Simple, all bits can be treated the same - Carry last position can be ignored - Subtraction by first complementing the subtrahend (minuend – subtrahend = difference) • Example: 0111 1101 ------10100 (7) (-3) + (4) 42 TI1400/11-PDS TU-Delft 1. Integer Representation 1.10. Overflow Addition/subtraction • 1C - all bits can be treated the same - when carry in last position add +1 to Least Significant Bit (LSB) 0111 1100 ------10011 1 -------0100 (7) (-3) + + (4) 43 TI1400/11-PDS TU-Delft 1. Integer Representation 1.10. Overflow Overflow detection • Suppose we have 2C notation - Carry-out MSB is not sufficient to indicate overflow 0111 (7) 0100 (4) -------- + 01011 (-5?) 44 TI1400/11-PDS TU-Delft 1. Integer Representation 1.10. Overflow Overflow • Overflow occurs in 2C iff - Both operand have the same sign - The sum bit in the MSB position has the opposite sign OVFL xn1yn1sn1 xn1yn1sn1 45 TI1400/11-PDS TU-Delft Data Type Representation 1. 2. 3. 4. 5. 6. Integer numbers 2.1. Fixed Point Real numbers 2.2. Floating Point Booleans 2.3. IEEE 754 Characters Composite types (e.g. arrays) Objects 46 TI1400/11-PDS TU-Delft 2.1. Fixed Point (Fractions) • Real numbers (R) = value in a continuous space (vs. Integers—discrete space) • F: -xn-120 + xn-22-1 + ....+ x 02-n+1 • Range: [-1, 1-2-n+1] • But: very small numbers cannot be represented, … 47 TI1400/11-PDS TU-Delft Question Can conversion between decimal and binary numbers always be done exacly in a. integers ? b. fractions ? 48 TI1400/11-PDS TU-Delft Answer a) Can conversion between decimal and binary numbers always be done exacly in integers? -> Yes b) Can conversion between decimal and binary numbers always be done exacly in fractions? -> No - Counter-example: 0.210 =0.00110011001100.....2 49 TI1400/11-PDS TU-Delft 2.2. Floating Point (1) • Need a represention for: - very large numbers - real numbers • With a limited number of bits • Floating point notation: G = m.re r = radix m = mantissa, significant, or fraction e = exponent 50 TI1400/11-PDS TU-Delft 2.2. Floating Point (2) • r is normally 10 or 2 • mantissa m is normalized: (1/r) ≤ m < 1 - e.g., if r = 2 then 0.5 ≤ m < 1 - e.g., if r = 10 then 6.543 x 1010 is normalized, 654,300 x 105 is not • Normalization is used to save as much as possible significant digits 51 TI1400/11-PDS TU-Delft 2.2. Floating Point Fixed or Floating Point? • Metrics - Range = ratio largest:smallest number - Precision = how many significant bits for representation • Floating point idea: separate the exponent from the mantissa. Vs Fixed point: - Wider range (through exponent) - Lower precision (mantissa has fewer bits) • There’s no free lunch in computer science! TI1400/11-PDS TU-Delft 2.3. FP Representation/IEEE 754 Birth of a Standard (IEEE 754) • Need - Many competing hardware makers IBM, Cray, CDC, Intel, … - Various range and accuracy • Work Group initiated by Intel 1976, led by W. Kahan - Correct FP - Developers can write portable code AND prove correctness • Result: IEEE 754 Read: IEEE 754: An Interview with William Kahan, IEEE Computer 1998 53 TI1400/11-PDS TU-Delft 2.3. FP Representation/IEEE 754 IEEE Floating point standard (IEEE 754) value = ±1s x 2e’-127 x m s e’ 8 bit m 32 bit 23 bit 54 TI1400/11-PDS TU-Delft 2.3. FP Representation/IEEE 754 Mantissa value = ±1s x 2e’-127 x m’ m’=1.m, m=[01][01]… • Implicit 1 bit just left of the point, so 24-bit total • Mantissa m is normalized: (1/r) ≤ m < 1 - So, including implicit 1., 1 ≤ m’ < 2 TI1400/11-PDS TU-Delft 2.3. FP Representation/IEEE 754 Exponent value = ±1s x 2e’-127 x m • e: [1,254] in excess-127 representation->[-126,127] • e=0 and e=255 are reserved values • Floating point (radix point is “floating”) - e=35 => m+1 means +235 (+34 359 738 368) - e=0 => m+1 means +20 (+1) TI1400/11-PDS TU-Delft 2.3. FP Representation/IEEE 754 Sign value = ±1s x 2e’-127 x m • Sign is 0 for positive, 1 for negative • Floating-point numbers that in value differ only in sign differ in IEEE 754 representation in the s bit TI1400/11-PDS TU-Delft Recap TI1400/11-PDS TU-Delft Lecture 2 Von Neumann Architecture Memory X: 1 Y: 2 Z: 3 • • READ(X) READ(Y) ADD(X,Y,Z) WRITE(Z) TI1400/11-PDS Central Processing Unit TEMP_A: TEMP_B: RESULT: IR: arithmetic unit Input CONTROL Output PC: TU-Delft Definition and Representation Integers (Z) = Set of natural numbers including 0 and their non-zero negatives Integer numbers are generally represented by: • An alfabet • A string X of n elements from : Xn n • Applying Xn to a valuation function F - signature: F : n Z 60 TI1400/11-PDS TU-Delft Criteria for representations • • • • Let Xn = < xn-1, xn-2 , ....., x0 > Range of natural numbers of X Representation of 010 X Efficiency of implementation - Sign inversion : A -A - Addition: A+B - Extension: n bit m bit representation, m>n 61 TI1400/11-PDS TU-Delft Data Type Representation 1. 2. 3. 4. 5. 6. Integer numbers 1.1. Sign-Magnitude 1.2. Two’s Complement (2C) Real numbers 1.3. One’s Complement (1C) Booleans 1.4. Excess-X Characters Composite types (e.g. arrays) Objects 62 TI1400/11-PDS TU-Delft 1. Integer Representation 1.9. Binary Addition Basic Adder xi ci+1 yi Full Adder ci si 63 TI1400/11-PDS TU-Delft 2.1. Fixed Point (Fractions) • Real numbers (R) = value in a continuous space (vs. Integers—discrete space) • F: -xn-120 + xn-22-1 + ....+ x 02-n+1 • Range: [-1, 1-2-n+1] • But: very small numbers cannot be represented, … 64 TI1400/11-PDS TU-Delft 2.3. FP Representation/IEEE 754 IEEE Floating point standard (IEEE 754) value = ±1s x 2e’-127 x m s e’ 8 bit m 32 bit 23 bit 65 TI1400/11-PDS TU-Delft Recap over TI1400/11-PDS TU-Delft Memory Jog 1: Select the correct answer 1 0011 0010 1101 1011 1101 11012 is … 1. 321BE0010 2. 123EBDD16 3. 231DB9916 4. 132DBDD16 TI1400/11-PDS TU-Delft Memory Jog 2: Select the correct answer 10510 is to 101012 as 07FD16 is to … 1. 40910 2. 204510 3. 90410 4. 204511 TI1400/11-PDS TU-Delft 2.3. FP Representation/IEEE 754 An Example: Number to IEEE 754 • • • • Input: 8.75 Step 1 (binary fixed-point): +1000.11 (.75=1x2-1+1x2-2) Step 2 (normalize): 1.0001100… x 23 Step 3 (bias the exponent): 3 + 127 = 130 (10000010) Source: C. Hecker, Let’s Go to the (Floating) Point, Game Developer, 1996 TI1400/11-PDS TU-Delft 2.3. FP Representation/IEEE 754 An Example: IEEE 754 to Number In: 110000000 01010000 00000000 0000000 • Step 1: Split into IEEE 754 components 1 10000000 01010000 00000000 0000000 • Step 2 (Sign): 0 is positive, 1 is negative. Sign is …? • Step 3 (exponent): 128 (10000000)-127 (bias)=1 • Step 4 (mantissa): 1.01010000… = 1.3125 (1x2-1=.5 1x2-2 =.25 1x2-3 =.125 1x2-4 =.0625) • Step 5 (result): (-1)1 x 1.3125 x 21 = -2.625 TI1400/11-PDS TU-Delft Excercise 1: Select the correct answer 6.510 in IEEE 754 Single Precision is … 1. 0 10000100 10101000000000000000000 2. 0 10000001 10100000000000000000000 3. 1 01000010 00101010100000000000000 4. 0 01000010 00101010100000000000002 TI1400/11-PDS TU-Delft Excercise 2: Select the correct answer 0 1000 0100 0101 0000 0000 0000 0000 000 is the IEEE 754 Single Precision for … 1. 3210 2. 4216 3. 4210 4. 3216 TI1400/11-PDS Hint: http://www.h-schmidt.net/FloatApplet/IEEE754.html TU-Delft 2.3. FP Representation/IEEE 754 FP exceptions [see wiki ieee 754] = ±1s x 2e’-126 x 0.fraction = ±1s x 2e’-127 x 1.fraction 0 00000000 1 00000000 0 11111111 1 11111111 0 11111111 TI1400/11-PDS 00000000000000000000000 00000000000000000000000 00000000000000000000000 00000000000000000000000 00000000000001000001000 = +0 =-0 = +Infinity = - Infinity = NaN TU-Delft 73 2.3. FP Representation/IEEE 754 Smallest Denormalized Positive Number In: 000000000 00000000 00000000 0000001 • Step 1: Split into IEEE 754 components 0 00000000 00000000 00000000 0000001 • • • • Step Step Step Step 2 3 4 5 TI1400/11-PDS (Sign): 0 is positive, 1 is negative. Sign is + (exponent): 0 (00000000)-126 (bias)=-126 (mantissa): 0.00…1 = 2-23(23 bits) (result): (-1)0 x 2-23 x 2-126 = 2-149 ~ 10-45 TU-Delft 2.3. FP Representation/IEEE 754 Smallest Normalized Positive Number In: 000000001 00000000 00000000 0000000 • Step 1: Split into IEEE 754 components 0 00000001 00000000 00000000 0000000 • • • • Step Step Step Step 2 (Sign): 0 is positive, 1 is negative. Sign is + 3 (exponent): 1 (00000001)-127 (bias)=-126 4 (mantissa): 1.00…0 = 1 5 (result): (-1)0 x 1 x 2-126 = 2-126 ~ 10-38 (smallest denormalized was 2-149 ~ 10-45) TI1400/11-PDS TU-Delft 2.3. FP Representation/IEEE 754 FP Operations • Multiplication f1 x f2: m1 r m2 r m1 m2 r e1 e2 e1 e 2 • Addition f1 + f2 (e2>e1, shift f1 by e1-e2): m1 r m2 r m1 r r m2 r (m1 r shift m2 ) r e 2 e1 e2 shift e2 e2 76 TI1400/11-PDS TU-Delft 2.3. FP Representation/IEEE 754 FP accuracy • Rounding: danger of false accuracy • e.g., 1.5*106 + 0.1 *10-3 1.500000 0.000000 0001 ------------------- + 1.500000 77 TI1400/11-PDS TU-Delft 2.3. FP Representation/IEEE 754 FP rounding • Rounding = convert to lower precision (w/ error) • Operations use guard bits -> used for rounding 0.101 0.110 --------- * 0.011 110 78 TI1400/11-PDS TU-Delft 2.3. FP Representation/IEEE 754 FP-Rounding [1/2] • Chopping: cut at guards bits (Round to zero) - error: 0 to almost -1 in LSB + Bias towards zero Truncation error • 0.011110 0.011 grows with the #ops • Von Neumann Rounding/Round to Nearest - all guard bits 0 cut; else LSB:=1 - error (-1,1) in LSB • 0.01000 0.010 error: 0 • 0.01110 0.011 error: 1/16 • 0.0101110 0.011 79 TI1400/11-PDS TU-Delft 2.3. FP Representation/IEEE 754 FP-Rounding [2/2] • Round to nearest EVEN number (default IE3754) - Add 1 to LSB if truncated MSB is 1 Decimal: - error [-1/2,1/2] in LSB • 10.5 10 • 0.001000 0.001 • 11.5 12 • 0.001011 0.001 • -10.5 -10 • 0.000111 0.001 • -11.5 -12 • 0.001111 0.010 • 0.001100 0.010 (to nearest EVEN number) • Round downward/upward/to nearest ODD number 80 TI1400/11-PDS TU-Delft 2.2. Floating Point Fixed or Floating Point? (Revisited) staff.ee.sun.ac.za/whsteyn/Papers2/CONPRA2299_final.pdf • Problem - What range is needed? - What precision is needed? - Need to replicate results later? (standard format needed) • Platform AND Algorithm AND Input - How to optimize instructions/memory requirements? - What can the Platform do for this Algorithm AND Input? • There’s no free lunch in computer science! TI1400/11-PDS TU-Delft Data Type Representation 1. 2. 3. 4. 5. 6. Integer numbers Real numbers Booleans Characters Composite types (e.g. arrays) Objects 82 TI1400/11-PDS TU-Delft 3. Booleans • Booleans have two values: true/false • Used in programming languages - if COND then ACTION1 else ACTION2 • Storage takes usually a complete byte - false is often represented as 00000000 - true by any other pattern 83 TI1400/11-PDS TU-Delft Data Type Representation 1. 2. 3. 4. 5. 6. Integer numbers Real numbers 4.1. ASCII Booleans 4.2. Unicode Characters 4.3. Processing Characters Composite types (e.g. arrays) Objects 84 TI1400/11-PDS TU-Delft 4. Characters 4.1. ASCII • American Standard Code for Information Interchange • ASCII set 128 Characters (7 bit) • Alphabet {a-zA-Z} • Numbers {0,...,9} • Special symbols, eg. <, =, >, $ • Punctuation marks, ! ?, . • Data transmission codes (non printable characters) 85 TI1400/11-PDS TU-Delft 4. Characters 4.1. ASCII • Designed 1960—1963 • Underlies the WWW (actually, as common part of UTF-8) 86 TI1400/11-PDS TU-Delft 4. Characters 4.1. ASCII An Example The Demo, http://sloan.stanford.edu/MouseSite/1968Demo.html TI1400/11-PDS TU-Delft 4. Characters 4.1. ASCII Raster Images as ASCII (Example) PPM Format Specification, http://netpbm.sourceforge.net/doc/ppm.html P3 # RGB.ppm 33 255 0 0 255 255 255 0 0 0 255 255 255 0 255 0 255 255 255 0 0 0 255 255 255 0 0 255 TI1400/11-PDS TU-Delft 4. Characters 4.2. Unicode • Large set of international alphabets represented • Subset ISO 10646 • 16 bits representation (see Joseph Becker, Unicode 1988, http://www.unicode.org/history/unicode88.pdf) “a simple unambiguous fixed-length character encoding” 89 TI1400/11-PDS TU-Delft 4. Characters 4.2. Unicode/UTF-8 • Variable-width encoding for Unicode (1-4 bytes) • 1 byte: First 128 characters are ASCII (US-ASCII) • 2 bytes: Next 1,920 characters encode Latin+diacritics, Greek, Cyrillic, Coptic, … • 3 bytes: Most others (Chinese, Hindi, tagalog/PH, …) • 4 bytes: Historical/Exotic variants (Klingon? Actually, Klingon has an unofficial 2-byte Unicode encryption, see www.evertype.com/standards/csur/klingon.html ) 90 TI1400/11-PDS TU-Delft 4. Characters 4.2. Unicode/UTF-8 An Example: The Romanian Alphabet • HTML: Ă or Ă http://bucovina.chem.tue.nl/romanian.utf8.htm 91 TI1400/11-PDS TU-Delft 4. Characters 4.3. Processing of a number [1/3] • User types a number, e.g. 123 (decimal) • Computer receives ASCII codes - ASCII-code of 1: 00110001 - ASCII-code of 2: 00110010 - ASCII-code of 3: 00110011 92 TI1400/11-PDS TU-Delft 4. Characters 4.3. Processing of a number [2/3] • Program converts codes to binary values 1*100 2*10 3*1 123 01100100 00010100 00000011 01111011 • Perform operation (e.g. subtract 50) 123 50 73 01111011 00110010 01001001 93 TI1400/11-PDS TU-Delft 4. Characters 4.3. Processing of a number [3/3] • The number 73 must be fed back to the screen 0 (*100) 7 (*10) 3 (*1) 00000000 00000111 00000011 • Computer sends following ASCII codes to screen - ASCII code of 0: - ASCII code of 7: 00110000 00110111 94 TI1400/11-PDS TU-Delft Data Type Representation 1. 2. 3. 4. 5. 6. Integer numbers Real numbers Booleans Characters Composite types (e.g. arrays) Objects 95 TI1400/11-PDS TU-Delft 5. Arrays [1/3] • Most programming languages support arrays A: array[0..N,0..M] of integer; • Must be mapped onto linear memory • Row major order • Column major order 96 TI1400/11-PDS TU-Delft 5. Arrays [2/3] array row major column major 97 TI1400/11-PDS TU-Delft 5. Arrays [3/3] • Storage method is stored as part of array descriptor: so called dope vector. begin address no of dimensions dim #1: low bound dim #1: high bound dim #1: multiplier dim #2: low bound etc TI1400/11-PDS address fixed element no of dimensions stride (skipped memory elements) 98 TU-Delft Data Type Representation 1. 2. 3. 4. 5. 6. Integer numbers Real numbers Booleans Characters Composite types (e.g. arrays) Objects 99 TI1400/11-PDS TU-Delft 6. Objects Records Record field 1: type 1; field 2: type 2; end record • Records can be stored with fixed off-set • Records can contain pointers to other data structures. Results in linked lists 100 TI1400/11-PDS TU-Delft 6. Objects Raster Images as Records (Example) • Simple, fixed representation: - Height, Width - RGB(Alpha) data 03 03 FF 00 00 FF FF FF 00 00 00 FF FF FF 00 FF 00 FF FF FF 00 00 00 FF FF FF 00 00 FF TI1400/11-PDS TU-Delft 6. Objects Raster Images as Records (Example) • Fixed-format representation with look-up table: - Height, Width - Color map/look-up table (here, RGB) - Image = index in color map per pixel 03 03 FF FF FF 00 00 00 FF 00 00 00 FF 00 00 00 FF 02 00 01 Map[02] = 00 03 00 FF 00 00 01 00 04 TI1400/11-PDS TU-Delft 6. Objects Raster Images as Records (Example) • Variable-format representation with look-up table: - Image: Height, Width, number of bits per color, … - Color map structure: RGB/ARGB, number of entries, … - Color map/look-up table - Image = index in color map per pixel TI1400/11-PDS TU-Delft Fun With Hexadecimal Numbers Color Codes [1/2] http://en.wikibooks.org/wiki/LaTeX/Colors TI1400/11-PDS TU-Delft Fun With Hexadecimal Numbers Color Codes [2/2] http://johncfish.com/bggallery/otherchart/index.htm http://www.visibone.com/ TI1400/11-PDS TU-Delft