07 Datarepresentation 150216185458 Conversion Gate02
07 Datarepresentation 150216185458 Conversion Gate02
Dilum Bandara
[email protected]
Outline
Representing numbers
Unsigned
Signed
Floating point
Representing characters & symbols
ASCII
Unicode
2
Data Representation in Computers
Data are stored in Registers
Registers are limited in number & size
n-bits
3
Data Representation
Qualitative
• Represents quality or
characteristics
• Not proportional to a
value
Data • Name, NIC no, index no,
Representation Address
Quantitative
• Quantifiable
• Proportional to value α
• No of students, marks for
CS2052, GPA 4
Data Representation (Cont.)
Signed
Integers
Unsigned
Quantitative
Signed
Data
Non-
integers
Unsigned
Qualitative
5
Number Systems
Decimal number system
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Binary number system
0, 1
Octal number system
0, 1, 2, 3, 4, 5, 6, 7
Hexadecimal number system
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
6
Quantitative Numbers
Integers
Unsigned 20
Signed +20, -20
Non-integers
Floating point numbers - 10.25, 3.33333…, 1/8 = 0.125
7
Signed Integers
We need a way to represent negative values
3 representations
Sign & Magnitude representation (S&M)
Complement method
Bias notation or Excess notation
8
1. Sign & Magnitude Representation
Magnitude (n-bits)
9
Example – Sign & Magnitude
If 8-bit register is used what are min & max
numbers?
What are 0000 0000 and 1000 0000 in decimal?
Representation of zero is not unique
10
Sign & Magnitude (Cont.)
Advantages
Sign reversal
Finding absolute value |a|
Flip sign bit
Disadvantage
Adding a negative of a number is not the same as
subtraction
e.g., add 2 and -3
Need different operations
11
2. Complement Method
Base = Radix
Radix r system means r number of symbols
e.g., binary numbers have symbols 0, 1
2 types
r’s complement
(r – 1)’s complement
Where r is radix (base) of number system
Examples
Decimal 9’s & 10’s complement
Binary 1’s & 2’s complement
12
Complement Method – Definition
Given a number m in base/radix r & having n
digits
(r – 1)’s complement of m is
(rn – 1) – m
r ’s complement of n is
(rn – 1) – m + 1 = rn – m
13
Example – Complement Method
If m = 5982 & n = 4 digits
9’s complement is
9999 - maximum representable no
5982–
4017
10’s complement
9999 or 10000
5982– 5982–
4017 4018
1+
4018
14
Example – Complement Method
If m = 382 & n = 3
-n = -382 =
999 999
382– 382-
617 617
1+
618
-382 = 617 or 618
Depending on which complement we use
These are called complementary pair
15
1’s complement
Calculated by
(2n – 1) – m
If m = 0101
1’s complement of m on a 4-bit system
1111
0101–
1010
This represents -5 in 1’s complement
16
Finding 1’s Complement – Short Cut
Invert each bit of m
Example
m= 00101011
1’s complement of m = 11010100
m= 00000000 = 0
1’s complement of m = 1 1 1 1 1 1 1 1 = -0
17
Addition with 1’s Complement
If results has a carry add it to LSB (Least
Significant Bit)
Example
Add 6 and -3 on a 3-bit system
6 = 110
-3 = 100+
= 1010
1+
011
18
2’s Complement
Doesn’t require end-around carry operation as in
1’s complement
2’s complement is formed by
Finding 1’s complement
Add 1 to LSB
New range is from -128 to +127
-128 because of +1 to negative value
19
Example – 2’s Complement
Find 2’s complement of 0101011
m =0101011
=1010100
________ 1+
2’s =1010101
Short-cut
1. Search for the 1st bit with value 1 starting from LSB
2. Inver all bits after 1st one
20
Example – 2’s Complement (Cont.)
Add 10 and -5 on a 4-bit system
5 = 0101
-5 = 1011
10 = 1010
-5 = 1 0 1 1+
10101 0101
Discard
21
1’s vs. 2’s Complement
1’s complement has 2 zeros (+0, -0)
Value range is less than 2’s complement
23
Source: www3.ntu.edu.sg/home/ehchua/programming/java/DataRepresentation.html
4-bit, 2’s Complement Adder
Subtractor
Source: www.instructables.com/id/How-to-Build-an-8-Bit-Computer/step7/The-
ALU/ 24
Detecting Negative Numbers &
Overflow
Check for MSB
To find magnitude
1’s complement
Flip all bits
2’s complement
Flip all bits + 1
Rules to detect overflows in 2’s complement
If sum of 2 positive numbers yields a negative result,
sum has overflowed
If sum of 2 negative numbers yields a positive result,
sum has overflowed
Else, no overflow 25
3. Bias Notation
Number range to be represented is given a
positive bias so that smallest unsigned value is
equal to -B
If bias is B
Value B in number range becomes zero
n
b 2
i 0
i
i
B
Radix
Sign (base)
Mantissa
28
Floating Point Numbers (Cont.)
Sign
s e
N ( 1) m.2 Exponent
Radix
Mantissa
Exponent Mantissa
29
IEEE Floating Point Standard (FPS)
2 standards
1. Single precision
32-bits 31 30 23 22 0
23-bit mantissa S Exponent Mantissa (bits 0-22)
8-bit exponent
1-bit sign
2. Double precision
64-bits
52-bit mantissa
11-bit exponent 63 62 52 51 0
1-bit sign
S Exponent Mantissa (bits 0-51)
30
IEEE Floating Point Standard (Cont.)
s31 e30 … ... e23 m22 mn-2 … … m1 m0
Exponent Mantissa
s E 127
N ( 1) [1.m] 2
E – 127 = e
E = e + 127
What is stored is E
31
Single Precision
23-bit mantissa
m ranges from 1 to (2 – 2-23 )
8-bit exponent
E ranges from 1 to 254
0 & 255 reserved to represent -∞, +∞, NaN
e ranges from -126 to +127
Maximum representable number
2 x 2127 = 2128
32
Review – Binary Fractions
What is 101.11012 in decimal?
22 x 1 + 21 x 0 + 20 x 1 + 2-1 x 1 + 2-2 x 1 + 2-3 x 0 + 2-4 x 1
= 4 x 1 + 2 x 0 + 1 x 1 + 0.5 x 1 + 0.25 x 1 + 0.125 x 0 + 0.0625 x 1
= 4 + 1 + 0.5 + 0.25 + 0.0625
= 5.8125
What is 5.812510 in binary?
Integer – 5 = 101
Fraction – Keep multiplying fraction until answer is 1
0.8125 x 2 = 1.625
0.625 x 2 = 1.25
0.25 x 2 = 0.5
0.5 x 2 = 1.0
0.8125 = .1101 33
10 2
Example – Single Precision
IEEE Single Precision Representation of 17.15 10
Find 17.1510 in binary
17.1510 = 10001.00100112
= 1.00010010011 x 24
E = e + 127
E = 4 + 127 = 131
34
Character Representation
Belong to category of qualitative data
Represent quality or characteristics
Includes
Letters a-z, A-Z
Digits 0-9
Symbols !, @ , *, /, &, #, $
Control characters <CR>, <BEL>, <ESC>, <LF>
35
Character Representation (Cont.)
With a single byte (8-bits) 256 characters can be
represented
Standards
ASCII – American Standard Code for Information
Interchange
EBCDIC – Extended Binary-Coded Decimal
Interchange Code
Unicode
36
ASCII
De facto world-wide standard
Used to represent
Upper & lower-case Latin letters
Numbers
Punctuations
Control characters
There are 128 standard ASCII codes
Can be represented by a 7 digit binary number
000 0000 through 111 1111
Plus parity bit
37
ASCII Table
ASCII ASCII Hex Symbol ASCII Hex Symbol
Hex Symbol
0 0 NUL 32 20 (space) 48 30 0
1 1 SOH 33 21 ! 49 31 1
2 2 STX 34 22 " 50 32 2
3 3 ETX 35 23 # 51 33 3
4 4 EOT 36 24 $ 52 34 4
5 5 ENQ 37 25 % 53 35 5
6 6 ACK 38 26 & 54 36 6
7 7 BEL 39 27 ' 55 37 7
8 8 BS 40 28 ( 56 38 8
9 9 TAB 41 29 ) 57 39 9
10 A LF 42 2A * 58 3A :
11 B VT 43 2B + 59 3B ;
12 C FF 44 2C , 60 3C <
13 D CR 45 2D - 61 3D =
14 E SO 46 2E . 62 3E >
15 F SI 47 2F / 63 3F ?
38
ASCII Table (Cont.)
ASCII Hex Symbol ASCII Hex Symbol ASCII Hex Symbol
64 40 @ 80 50 P 96 60 `
65 41 A 81 51 Q 97 61 a
66 42 B 82 52 R 98 62 b
67 43 C 83 53 S 99 63 c
68 44 D 84 54 T 100 64 d
69 45 E 85 55 U 101 65 e
70 46 F 86 56 V 102 66 f
71 47 G 87 57 W 103 67 g
72 48 H 88 58 X 104 68 h
73 49 I 89 59 Y 105 69 i
74 4A J 90 5A Z 106 6A j
75 4B K 91 5B [ 107 6B k
76 4C L 92 5C \ 108 6C l
77 4D M 93 5D ] 109 6D m
78 4E N 94 5E ^ 110 6E n
79 4F O 95 5F _ 111 6F o
39
ASCII – Things to Note
ASCII codes for digits aren’t equal to numeric
value
Uppercase & lowercase alphabetic codes differ
by 0x20
Shift key clears bit 5
0x20 = 32 = 0010 0000
Example:
A = 65 = 0100 0001
a = 97 = 0110 0001
40
Unicode (www.unicode.org)
Designed to overcome limitation of number of
characters
Assigns unique character codes to characters in
a wide range of languages
A 16-bit character set
UCS-2
UCS-4 is 32-bit
65,536 (216) distinct Unicode characters
Unicode provides a unique number for every character,
no matter what the platform,
no matter what the program,
no matter what the language 41
Unicode (Cont.)
Source: www3.ntu.edu.sg/home/ehchua/programming/java/DataRepresentation.html
42
Unicode – Sinhala & Tamil
43