DELFT UNIVERSITY OF TECHNOLOGY - TU Delft: Toegepaste Wiskunde
TU-Delft TI1400/11-PDS 1 Arithmetic and Data Representation iosup/Courses/2011_ti1400_3.ppt.
-
date post
20-Dec-2015 -
Category
Documents
-
view
217 -
download
0
Transcript of TU-Delft TI1400/11-PDS 1 Arithmetic and Data Representation iosup/Courses/2011_ti1400_3.ppt.
TU-DelftTI1400/11-PDS
1
Arithmetic and Data Representation
http://www.pds.ewi.tudelft.nl/~iosup/Courses/2011_ti1400_3.ppt
TU-DelftTI1400/11-PDS
Lecture 2Programmable device
2,1 3ProgrammableDevice
input stream output stream
programREAD(X)READ(Y)ADD(X,Y,Z)WRITE(Z)
• 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
TU-DelftTI1400/11-PDS
Lecture 2Von Neumann Architecture
READ(X)READ(Y)ADD(X,Y,Z)WRITE(Z)
X: 1Y: 2Z: 3 • •
TEMP_A: TEMP_B: RESULT:
IR:
PC:
arithmeticunit
Central Processing Unit
CONTROL
Memory
Input
Output
TU-DelftTI1400/11-PDS
Problem: How to Represent and Use Data?
1. Representation2. Arithmetic3. Conversion
TU-DelftTI1400/11-PDS
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)
TU-DelftTI1400/11-PDS
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)
TU-DelftTI1400/11-PDS
9
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 16-bit 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
TU-DelftTI1400/11-PDS
14
Data Type Representation
1. Integer numbers2. Real numbers3. Booleans4. Characters5. Composite types (e.g. arrays)6. Objects
TU-DelftTI1400/11-PDS
17
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
TU-DelftTI1400/11-PDS
18
Examples
• R = {I, V, M, ....}- VI = 6- IV = 4
• 10 = {0, 1, 2, 3, .....,9}- 27710 = 2*102 + 7*101 + 7*100
• Many sets and valuation functions F possible
radix
TU-DelftTI1400/11-PDS
20
Binary representation
• In computer, we have 2 ={0, 1}
• There are, however, many valuation functions F
277
-15
66
1110
0011
1010
F1
F2
F3
TU-DelftTI1400/11-PDS
21
Criteria for representations
• Let Xn = < xn-1, xn-2 , ....., x0 >• Range of natural numbers of X• Representation of 010
• Efficiency of implementation- Sign inversion : A -A- Addition: A+B- Extension: n bit m bit representation, m>n
X
TU-DelftTI1400/11-PDS
22
Data Type Representation
1. Integer numbers2. Real numbers3. Booleans4. Characters5. Composite types (e.g. arrays)6. Objects
1.1. Sign-Magnitude1.2. Two’s Complement (2C)1.3. One’s Complement (1C)1.4. Excess-X
TU-DelftTI1400/11-PDS
23
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
1 1 0 V = - 21 = -2
0 1 0 V = +21 = 2sign bit
X
TU-DelftTI1400/11-PDS
24
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)
1 1 0 V = - 22 +21 = -2
0 1 0 V = +21 = 2sign
X
TU-DelftTI1400/11-PDS
010
25
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 0011
+
TU-DelftTI1400/11-PDS
26
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...
1 1 0
0 1 0sign
X V = - 22 +1+21 = -1
V = +21 = 2
TU-DelftTI1400/11-PDS
27
1. Integer Representation 1.3. 1C 1C negative
100 101 110 111 000 001 010 011-3 -2 -1 0 0 1 2 3
Inversion rule:- invert all bits- General: A + Å = 2n-1
110 001
TU-DelftTI1400/11-PDS
28
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.....
1 1 0
0 1 0
X V = 22 + 21 - 22 = 2
V = +21 - 22= -2
TU-DelftTI1400/11-PDS
29
Data Type Representation
1. Integer numbers2. Real numbers3. Booleans4. Characters5. Composite types (e.g. arrays)6. Objects
1.5. Addition1.6. Sign Extension for 2C1.7. Other Representations1.8. Conversion
TU-DelftTI1400/11-PDS
30
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
TU-DelftTI1400/11-PDS
31
1. Integer Representation 1.5. AdditionExample
X000001010011100101110111
2C0123-4-3-2-1
1C0123-3-2-10
SM01230-1-2-3
Ex-4-4-3-2-10123
TU-DelftTI1400/11-PDS
32
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=44 = 000100 -> 01009 = 001001 -> overflow-6 = 111010 -> 1010
TU-DelftTI1400/11-PDS
33
1. Integer Representation 1.7. Other RepresentationsBinary Coded Decimaldecimal
0123456789
X0000000100100011010001010110011110001001
TU-DelftTI1400/11-PDS
34
Radix 2, 8, 10, 16binair
01
1011
100101110111
10001001101010111100110111101111
octal01234567
1011121314151617
dec.0123456789
101112131415
hex0123456789ABCDEF
L33T-Speak
http://www.jayssite.com/stuff/l33t/l33t_translator.html
h4x0r
1007
1337
hacker
money (loot)
elite (leet)
1. Integer Representation 1.7. Other Representations
TU-DelftTI1400/11-PDS
35
1. Integer Representation
1.8. Conversion [1/2]
• Simple, if one radix is power of other radix- binary to octal10100101001 = 010 100 101 001 = 2451oct
- binary to hexadecimal10100101001 = 0101 1010 1001 = 5A9hex =
0x05A9
TU-DelftTI1400/11-PDS
36
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 724 = 2*362 +0 ...... =...... 1 = 0*1 +1
LSB
TU-DelftTI1400/11-PDS
37
Data Type Representation
1. Integer numbers2. Real numbers3. Booleans4. Characters5. Composite types (e.g. arrays)6. Objects
1.9. Binary Addition1.10. Operation Overflow
TU-DelftTI1400/11-PDS
38
1. Integer Representation 1.9. Binary AdditionAddition of binary digits
• Addition of two bits
• Sum s and Carry c
000
+
101
+
011
+
1 1 10
+
x yc s
+
TU-DelftTI1400/11-PDS
39
1. Integer Representation 1.9. Binary AdditionMore positions
xi
yi
ci
si
ci+1
.
...
. .
.
xi
001
yi
011
ci
011
si
001
ci+1
011
truth table (partial)
TU-DelftTI1400/11-PDS
40
1. Integer Representation 1.9. Binary AdditionBasic Adder
Full Adder
xi yi
ci
si
ci+1
TU-DelftTI1400/11-PDS
42
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 (7) 1101 (-3)------- +10100 (4)
TU-DelftTI1400/11-PDS
43
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 (7) 1100 (-3)------- +10011 1-------- + 0100 (4)
TU-DelftTI1400/11-PDS
44
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?)
TU-DelftTI1400/11-PDS
45
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 xn 1yn 1s n 1 x n 1y n 1sn 1
TU-DelftTI1400/11-PDS
46
Data Type Representation
1. Integer numbers2. Real numbers3. Booleans4. Characters5. Composite types (e.g. arrays)6. Objects
2.1. Fixed Point2.2. Floating Point2.3. IEEE 754
TU-DelftTI1400/11-PDS
47
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,
…
TU-DelftTI1400/11-PDS
48
Question
Can conversion between decimal and binary numbers always be done exacly in
a. integers ?b. fractions ?
TU-DelftTI1400/11-PDS
49
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
TU-DelftTI1400/11-PDS
50
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 = radixm = mantissa, significant, or fractione = exponent
TU-DelftTI1400/11-PDS
51
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
TU-DelftTI1400/11-PDS
2.2. Floating PointFixed 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!
TU-DelftTI1400/11-PDS
Birth of a Standard (IEEE 754)
53
2.3. FP Representation/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
TU-DelftTI1400/11-PDS
IEEE Floating point standard (IEEE 754)
54
2.3. FP Representation/IEEE 754
e’
ms
32 bit23 bit8 bit
value = ±1s x 2e’-127 x m
TU-DelftTI1400/11-PDS
2.3. FP Representation/IEEE 754
Mantissa
• 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
value = ±1s x 2e’-127 x m’
m’=1.m, m=[01][01]…
TU-DelftTI1400/11-PDS
2.3. FP Representation/IEEE 754
Exponent
• 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)
value = ±1s x 2e’-127 x m
TU-DelftTI1400/11-PDS
2.3. FP Representation/IEEE 754
Sign
• 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
value = ±1s x 2e’-127 x m
TU-DelftTI1400/11-PDS
Lecture 2Von Neumann Architecture
READ(X)READ(Y)ADD(X,Y,Z)WRITE(Z)
X: 1Y: 2Z: 3 • •
TEMP_A: TEMP_B: RESULT:
IR:
PC:
arithmeticunit
Central Processing Unit
CONTROL
Memory
Input
Output
TU-DelftTI1400/11-PDS
60
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
TU-DelftTI1400/11-PDS
61
Criteria for representations
• Let Xn = < xn-1, xn-2 , ....., x0 >• Range of natural numbers of X• Representation of 010
• Efficiency of implementation- Sign inversion : A -A- Addition: A+B- Extension: n bit m bit representation, m>n
X
TU-DelftTI1400/11-PDS
62
Data Type Representation
1. Integer numbers2. Real numbers3. Booleans4. Characters5. Composite types (e.g. arrays)6. Objects
1.1. Sign-Magnitude1.2. Two’s Complement (2C)1.3. One’s Complement (1C)1.4. Excess-X
TU-DelftTI1400/11-PDS
63
1. Integer Representation 1.9. Binary AdditionBasic Adder
Full Adder
xi yi
ci
si
ci+1
TU-DelftTI1400/11-PDS
64
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,
…
TU-DelftTI1400/11-PDS
IEEE Floating point standard (IEEE 754)
65
2.3. FP Representation/IEEE 754
e’
ms
32 bit23 bit8 bit
value = ±1s x 2e’-127 x m
TU-DelftTI1400/11-PDS
Memory Jog 1: Select the correct answer
1 0011 0010 1101 1011 1101 11012 is …1.321BE0010
2.123EBDD16
3.231DB9916
4.132DBDD16
TU-DelftTI1400/11-PDS
Memory Jog 2: Select the correct answer
10510 is to 101012 as 07FD16 is to …1.40910
2.204510
3.90410
4.204511
TU-DelftTI1400/11-PDS
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
TU-DelftTI1400/11-PDS
2.3. FP Representation/IEEE 754
An Example: IEEE 754 to Number
• Step 1: Split into IEEE 754 components
• 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
1 10000000 01010000 00000000 0000000
110000000 01010000 00000000 0000000In:
TU-DelftTI1400/11-PDS
Excercise 1: Select the correct answer6.510 in IEEE 754 Single Precision is …
1.0 10000100 10101000000000000000000
2.0 10000001 10100000000000000000000
3.1 01000010 00101010100000000000000
4.0 01000010 00101010100000000000002
TU-DelftTI1400/11-PDS
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.3216Hint: http://www.h-schmidt.net/FloatApplet/IEEE754.html
TU-DelftTI1400/11-PDS
73
2.3. FP Representation/IEEE 754
FP exceptions [see wiki ieee 754]
0 00000000 00000000000000000000000 = +01 00000000 00000000000000000000000 = - 00 11111111 00000000000000000000000 = +Infinity1 11111111 00000000000000000000000 = - Infinity0 11111111 00000000000001000001000 = NaN
= ±1s x 2e’-126 x 0.fraction = ±1s x 2e’-127 x 1.fraction
TU-DelftTI1400/11-PDS
2.3. FP Representation/IEEE 754
Smallest Denormalized Positive Number
• Step 1: Split into IEEE 754 components
• Step 2 (Sign): 0 is positive, 1 is negative. Sign is +
• Step 3 (exponent): 0 (00000000)-126 (bias)=-126• Step 4 (mantissa): 0.00…1 = 2-23(23 bits)• Step 5 (result): (-1)0 x 2-23 x 2-126 = 2-149 ~ 10-45
0 00000000 00000000 00000000 0000001
000000000 00000000 00000000 0000001In:
TU-DelftTI1400/11-PDS
2.3. FP Representation/IEEE 754
Smallest Normalized Positive Number
• Step 1: Split into IEEE 754 components
• Step 2 (Sign): 0 is positive, 1 is negative. Sign is +• Step 3 (exponent): 1 (00000001)-127 (bias)=-126• Step 4 (mantissa): 1.00…0 = 1• Step 5 (result): (-1)0 x 1 x 2-126 = 2-126 ~ 10-38
(smallest denormalized was 2-149 ~ 10-45)
0 00000001 00000000 00000000 0000000
000000001 00000000 00000000 0000000In:
TU-DelftTI1400/11-PDS
76
2.3. FP Representation/IEEE 754
FP Operations
• Multiplication f1 x f2:
• Addition f1 + f2 (e2>e1, shift f1 by e1-e2):
m1 re1 m2 re2 m1 m2 re1 e2
m1 re1 m2 re2 m1 r shift re2 m2 re2
(m1 r shift m2)re2
TU-DelftTI1400/11-PDS
77
2.3. FP Representation/IEEE 754
FP accuracy
• Rounding: danger of false accuracy• e.g., 1.5*106 + 0.1 *10-3
1.5000000.000000 0001------------------- +1.500000
TU-DelftTI1400/11-PDS
78
2.3. FP Representation/IEEE 754
FP rounding
• Rounding = convert to lower precision (w/ error)
• Operations use guard bits -> used for rounding
0.1010.110--------- *0.011 110
TU-DelftTI1400/11-PDS
79
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
• 0.011110 0.011• 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
Truncation error grows with the #ops
TU-DelftTI1400/11-PDS
80
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- error [-1/2,1/2] in LSB
• 0.001000 0.001 • 0.001011 0.001• 0.000111 0.001• 0.001111 0.010• 0.001100 0.010 (to nearest EVEN number)
• Round downward/upward/to nearest ODD number
Decimal:• 10.5 10• 11.5 12• -10.5 -10• -11.5 -12
TU-DelftTI1400/11-PDS
2.2. Floating PointFixed or Floating Point? (Revisited)
• 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!
staff.ee.sun.ac.za/whsteyn/Papers2/CONPRA2299_final.pdf
TU-DelftTI1400/11-PDS
82
Data Type Representation
1. Integer numbers2. Real numbers3. Booleans4. Characters5. Composite types (e.g. arrays)6. Objects
TU-DelftTI1400/11-PDS
83
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
TU-DelftTI1400/11-PDS
84
Data Type Representation
1. Integer numbers2. Real numbers3. Booleans4. Characters5. Composite types (e.g. arrays)6. Objects
4.1. ASCII4.2. Unicode4.3. Processing Characters
TU-DelftTI1400/11-PDS
85
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)
TU-DelftTI1400/11-PDS
86
4. Characters
4.1. ASCII• Designed 1960—1963 • Underlies the WWW (actually, as common part of UTF-8)
TU-DelftTI1400/11-PDS
4. Characters 4.1. ASCII An Example
The Demo, http://sloan.stanford.edu/MouseSite/1968Demo.html
TU-DelftTI1400/11-PDS
4. Characters 4.1. ASCII Raster Images as ASCII (Example)P3 # RGB.ppm 3 3 255 0 0 255 255 255 0 0 0 255 255 255 0 255 0 255 255 2550 0 0 255 255 255 0 0 255
PPM Format Specification, http://netpbm.sourceforge.net/doc/ppm.html
TU-DelftTI1400/11-PDS
89
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”
TU-DelftTI1400/11-PDS
90
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 )
TU-DelftTI1400/11-PDS
91
4. Characters 4.2. Unicode/UTF-8 An Example: The Romanian Alphabet
• HTML:Ă orĂ
http://bucovina.chem.tue.nl/romanian.utf8.htm
TU-DelftTI1400/11-PDS
92
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
TU-DelftTI1400/11-PDS
93
4. Characters
4.3. Processing of a number [2/3]
• Program converts codes to binary values1*100 011001002*10 000101003*1 00000011123 01111011
• Perform operation (e.g. subtract 50)123 0111101150 0011001073 01001001
TU-DelftTI1400/11-PDS
94
4. Characters
4.3. Processing of a number [3/3]
• The number 73 must be fed back to the screen0 (*100) 000000007 (*10) 000001113 (*1) 00000011
• Computer sends following ASCII codes to screen- ASCII code of 0: 00110000- ASCII code of 7: 00110111
TU-DelftTI1400/11-PDS
95
Data Type Representation
1. Integer numbers2. Real numbers3. Booleans4. Characters5. Composite types (e.g. arrays)6. Objects
TU-DelftTI1400/11-PDS
96
5. Arrays [1/3]
• Most programming languages support arraysA: array[0..N,0..M] of integer;
• Must be mapped onto linear memory• Row major order• Column major order
TU-DelftTI1400/11-PDS
98
5. Arrays [3/3]
• Storage method is stored as part of array descriptor: so called dope vector.
begin addressno of dimensionsdim #1: low bounddim #1: high bounddim #1: multiplierdim #2: low boundetc
address fixed elementno of dimensionsstride (skipped memory
elements)
TU-DelftTI1400/11-PDS
99
Data Type Representation
1. Integer numbers2. Real numbers3. Booleans4. Characters5. Composite types (e.g. arrays)6. Objects
TU-DelftTI1400/11-PDS
100
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
TU-DelftTI1400/11-PDS
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 FF00 00 00 FF FF FF 00 00 FF
TU-DelftTI1400/11-PDS
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 pixel03 03
FF FF FF 00 00 00 FF 00 00 00 FF 00 00 00 FF
02 00 0100 03 0001 00 04
Map[02] = FF 00 00
TU-DelftTI1400/11-PDS
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
TU-DelftTI1400/11-PDS
Fun With Hexadecimal NumbersColor Codes [1/2] http://en.wikibooks.org/wiki/LaTeX/Colors