computer structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · computer structure...

116
Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura de Computadores, Comunicaciones y Sistemas UNIVERSIDAD CARLOS III DE MADRID Unit 2. Data representation

Upload: others

Post on 10-Aug-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Computer Structure

Unit 2. Data representation

Departamento de InformáticaGrupo de Arquitectura de Computadores, Comunicaciones y Sistemas

UNIVERSIDAD CARLOS III DE MADRID

Unit 2. Data representation

becweb
UC3M
becweb
Texto escrito a máquina
Félix García Carballeira, Alejandro Calderón Mateos, José Daniel García Sánchez
Page 2: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Contents

� Concept of a computer� Introduction to information representation

� Types of information� Positional number systems

� Representations� Representations� Characters� Numbers� Float point

� Standard: IEEE 754

ARCOS Computer Structure 2

Page 3: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

What is a computer?

Processordata

Instructions

results

ARCOS Computer Structure 3

Instructions

All information is represented using the binary system

Page 4: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Types of information

� Machine instructions

� Data� Characters � Numbers without sign� Numbers without sign� Numbers with sign� Real numbers

ARCOS Computer Structure 4

ProcessorData

Instructions

Results

Page 5: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Format of a machine instruction

001 AB 00000000101

ARCOS Computer Structure 5

Operation Code

OperandsRegisters

Memory addresses

Numbers

Page 6: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Positional number systems

� A number is a chain of digits. Each digit has a scale factor according to the position in the chain

� Let b be a numeration base, a number X is defined as:

b21012 )xx,xxx(X ⋅⋅⋅⋅⋅⋅= −−

The decimal value of X is:

⋅⋅⋅⋅+⋅+⋅+⋅+⋅⋅⋅⋅=⋅= −−

−−

+∞

−∞=∑ 2

21

10

01

12

2i

i

i xbxbxbxbxbxbV(X)

b21012 )xx,xxx(X ⋅⋅⋅⋅⋅⋅= −−

0 ≤ xi < b

ARCOS Computer Structure 6

Page 7: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Positional number systems

� BinaryX = 1 0 1 0 0 1 0 1

... 27 26 25 24 23 22 21 20

� HexadecimalY = 0x F 1 F A 8 0

... 165 164 163 162 161 160... 165 164 163 162 161 160

� From binary to hexadecimal:� Make groups of 4 bits from right to left� A group of 4 bits represents the value of the hexadecimal digit� Ex.: 1 0 1 0 0 1 0 1

0x A 5

ARCOS Computer Structure 7

Page 8: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Positional number systems

� How many “values” (codes) can be represented using n bits?

� How many bits are needed to represent m “values” (codes)?

ARCOS Computer Structure 8

Page 9: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Positional number systems

� How many “values” (codes) can be represented using n bits?� 2n

� 256 codes can be represented using 8 bits.

� How many bits are needed to represent m “values” (codes)?� How many bits are needed to represent m “values” (codes)?� Log2(n) � We need 6 bits to represent 35 codes

� Using n bits� The minimum value that can be represented is 0� The maximum value that can be represented is 2n-1

ARCOS Computer Structure 9

Page 10: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Example

� Represent 342 using the binary system:

weights: 256 128 64 32 16 8 4 2 1? ? ? ? ? ? ? ? ?

ARCOS Computer Structure 10

Page 11: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Example

� Represent 342 using the binary system:

weights : 256 128 64 32 16 8 4 2 11 0 1 0 1 0 1 1 0

342-256=86 86-64=22 22-16=6 6-4=2 2-2=0

ARCOS Computer Structure 11

Page 12: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Example

� Compute the decimal value of 23 1´s:

111111111111111111111112

ARCOS Computer Structure 12

Page 13: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Example

� Compute the decimal value of 23 1´s:

111111111111111111111112

X = 223 - 1

11111111111111111111111 2 = X + 00000000000000000000001 2 = 1

100000000000000000000000 2 = 2 23

X = 2 23 - 1

ARCOS Computer Structure 13

Page 14: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Addition example

1 0 1 0

+ 0 1 1 1

11

ARCOS Computer Structure 14

+ 0 1 1 1

1 0 0 0 1

Page 15: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Prefix

Number Abr Factor IS

Kilo K 210 = 1,024 103 = 1,000

Mega M 220 = 1,048,576 106 = 1,000,000

Giga G 230 = 1,073,741,824 109 = 1,000,000,000

Tera T 240 = 1,099,511,627,776 1012 = 1,000,000,000,000

Peta P 250 = 1,125,899,906,842,624 1015 = 1,000,000,000,000,000

Exa E 260 = 1,152,921,504,606,846,976 1018 = 1,000,000,000,000,000,000

ARCOS Computer Structure 15

� 1 KB = 1024 bytes, but in IS is 1000 bytes� Hard drives and manufactures and telecommunications use the IS

� A hard drive of 30 GB stores 30 x 109 bytes� A 1 Mbit/s network transmits 106 bps.

Exa E 2 = 1,152,921,504,606,846,976 10 = 1,000,000,000,000,000,000

Zetta Z 270 = 1,180,591,620,717,411,303,424 1021 = 1,000,000,000,000,000,000,000

Yotta Y 280 = 1,208,925,819,614,629,174,706,176

1024 = 1,000,000,000,000,000,000,000,000

Page 16: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Example

� How many bytes can store a 200 GB hard drive?

� How many bytes per second does transmit an ADSL of 20 Mb?Mb?

ARCOS Computer Structure 16

Page 17: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Solution

� How many bytes can store a 200 GB hard drive?� 200 GB = 200 * 109bytes = 186.26 Gigabytes

� How many bytes per second does transmit an ADSL of 20 Mb?Mb?� B → Byte � b → bit.� 20 Mb = 20 * 106bits = 20 * 106 / 8 bytes = 2.38 Megabytes

per second

ARCOS Computer Structure 17

Page 18: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Typical sizes

� Octet, char or byte� Character representation� 8 bits typically

� Word� Information that can be managed in parallel in the computer� Information that can be managed in parallel in the computer� Typically 32, 64 bits

� Half word� Double word

ARCOS Computer Structure 18

Page 19: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Representation of characters

� Each char is coded using a byte.

� Using n bits ⇒⇒⇒⇒ 2n possible chars:� 6 bits(64 chars)

� 26 letters (A...Z), 10 numbers(0...9), marks (. , ; : ...) y specials(+ - [ ...)� Example: BCDIC

� 7 bits(128 chars)� Includes uppercase, lowercase, control chars� Includes uppercase, lowercase, control chars� Example : ASCII

� 8 bits(256 chars)� Includes accented letters, ñ, semigraphical chars� Example : EBCDIC y extended ASCII

� 16 bits(34.168 chars )� Different languages (Chinese ,...)� Example: UNICODE

ARCOS Computer Structure 19

Page 20: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Representation of characters

� Systems:� EBCDIC (8 bits)� ASCII (8 bits)� Unicode (8 bits)

� Each char is represented using a numerical code

ARCOS Computer Structure 20

Page 21: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Example: ASCII table (7 bits)

Computer Structure 21ARCOS

Page 22: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

ASCII code. Properties

� Chars from ‘0’ to ‘9’ are consecutives� Simplify checking a digit� Simplify the operation to obtain a value� Why?

� Uppercases and lowercases differ in only one bit � Uppercases and lowercases differ in only one bit � Simplify conversions

� Control chars are located in a range� Simplify the interpretation

ARCOS Computer Structure 22

Page 23: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Strings

� Fixed length strings

� Variable length strings with a separator

� Variable length strings with the length in the header� Variable length strings with the length in the header

ARCOS Computer Structure 23

Page 24: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Fixed length strings

ARCOS Computer Structure 24

Page 25: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Variable length strings with the size

ARCOS Computer Structure 25

Page 26: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Variable length strings with a separator

ARCOS Computer Structure 26

Page 27: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Numbers representation

� Numbers:� Natural numbers: 0, 1, 2, 3, ...

� Integers (signed numbers): ... -3, -2, -1, 0, 1, 2, 3, ....

� Rational numbers: fractions(5/2 = 2,5)

� Irrational numbers: 21/2, π, e, ...

� Infinite sets and a fixed representation space� It is impossible to represent all numbers

Computer Structure 27ARCOS

Page 28: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Problems with the number representation

in a computer

� Any set of numbers is infinite

� Irrational numbers are not representabled by infinite digits required

� Finite physical space representation

� A sequence of n bits can represent 2n different codes

ARCOS Computer Structure 28

Page 29: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Representation features

� Representation range: � Interval between the lowest and highest numbers

� Precision:� Not all numbers can be represented accurately

� Resolution: � Resolution:

� Difference between a representable number and the immediately following

� Resolution= maximum error in the representation

� Resolution can be:� The resolution can be constant throughout the range.

� Variable (float point)

ARCOS Computer Structure 29

Page 30: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Typical number representation systems

� Natural numbers (no sign)� Fixed point without sign or binary

� Signed numbers (sign):� Sign-magnitud� Sign-magnitud� One’s complement� Two’s complement� Excess representation

� Real numbers� Float point: IEEE Standard 754

ARCOS Computer Structure 30

Page 31: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Binary representation

� Positional system in base 2

0n bits

n-1

i

1n

0i

i x2V(X) ⋅= ∑−

=

Range: [0, 2n -1]• Range: [0, 2n -1]• Resolution: 1 unit

Computer Structure 31ARCOS

Page 32: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Signed number representations

� Sign-magnitud� One’s complement� Two’s complement� Excess representation

ARCOS Computer Structure 32

Page 33: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Sign-magnitude

� Use a sign bit (S) (0 ⇒ +; 1 ⇒ -)

0Magnitude (n-1 bits)

n-1 n-2S

2ni x2V(X) ⋅=∑

Si x = 0

Range: [-2n-1 +1, 2n-1 -1]Range: [-2n-1 +1, 2n-1 -1]Resolution: 1 unit

i0i

i x2V(X) ⋅=∑=

i

2n

0i

i x2V(X) ⋅−= ∑−

=

i

2n

0i

i1n x2)x21(V(X) ⋅⋅⋅−= ∑

=−

Si x n-1= 0

Si x n-1= 1⇒⇒⇒⇒

Computer Structure 33ARCOS

Page 34: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Example

� With n = 6 bits

� The number 7 is coded as: 00111 � First bit is the sign

� The number -7 is coded as: 10111� First bit is the sign

ARCOS Computer Structure 34

Page 35: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Example

� Can we represent the number 745 in sign-magnitude with 10 bits?

ARCOS Computer Structure 35

Page 36: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Example

� Can we represent the number 745 in sign-magnitude with 10 bits?

� Solution:� With 10 bits the range in sign-magnitude is:With 10 bits the range in sign-magnitude is:

[-29+1,…,-0,+0,….29-1] ⇒ [-511, 511]then, we cannot represent 745

ARCOS Computer Structure 36

Page 37: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Sign-magnitude problems

� Two codes to represent 0:� With n = 5 bits:

� 00000 represent 0� 10000 represent 0

� Different circuits for addition/substraction

ARCOS Computer Structure 37

Page 38: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

One’s complement

� Positive number: � Is coded in binary with n-1 bits

Magnitude(n-1 bits) n-1 n-2 00

ARCOS Computer Structure 38

Range: [0, 2n-1 -1]• Range: [0, 2n-1 -1]• Resolution: 1 unit

Magnitude(n-1 bits) 0

i

2n

0i

ii

1n

0i

i x2x2V(X) ⋅=⋅= ∑∑−

=

=

Page 39: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

One’s complement

� Negative number: � The number X < 0 is coded as 2n – X - 1

� Is complemented: change 0´s by 1´s and 1´s by 0´s� The number has 1 in the first bit

� This bit is nota signed bit, it belongs to the number

ARCOS Computer Structure 39

Range: [-2n-1+1, -0]• Range: [-2n-1+1, -0]• Resolution: 1 unit

122V(X) i

1

0i

i −⋅+−= ∑−

=

xn

n

n-1 n-2 01

Page 40: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

One’s complement

� First bit in positive numbers is 0� First bit in negative numbers is 1

ARCOS Computer Structure 40

00000 00001 01111...

111111111010000 ...

0 representation

Page 41: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Example

� With n = 5 bits� X = 5

� Is coded in one’s complement as:� 00101

� X = -5 � X = -5 � Is coded in one’s complement as:

� How is the value of 00111 in one’s complement?� It is a positive number, the value is 7

� How is the value of 11000 in one’s complement?� It is negative, the number is complemented: 00111 (7)

� The value is -7

ARCOS Computer Structure 41

Page 42: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Addition and subtraction

� With n = 5 bits

� X = 5

� In one’s complement = 00101

� Y = 7

In one’s complement = 00111� In one’s complement = 00111

� ¿X + Y?

X = 00101

Y = 00111+

X+Y = 01100

� The value 01100 in one’s complement is 12

ARCOS Computer Structure 42

Page 43: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Addition and subtraction

� With n = 5 bits

� X = -5

� In one’s complement = complement of 00101: 11010

� Y = -7

� In one’s complement = complement of 00111: 11000

� ¿X + Y?

-X = 11010

-Y = 11000+

-(X+Y) = 110010 A carry is generated and is added

1

10011

� The value of 10011 in one’s complement is negative and the complement is -01100 = - 12

ARCOS Computer Structure 43

Page 44: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Why the carry is discarded?

� -X is coded as 2n – X – 1� -Y is coded as 2n – Y – 1� -(X + Y) is coded as 2n – (X+Y) - 1

� When –X – Y is added, we obtain:� When –X – Y is added, we obtain:

-X = 2n – X – 1

-Y = 2n – Y – 1

-(X+Y) = 2n + 2n – (X + Y) – 2

The result is corrected discarding the carry

2n (a carry bit) and adding it to the result

=> 2n – (X + Y) – 1

ARCOS Computer Structure 44

Page 45: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

One’s complement problems

� Multiple representations of 0� With n = 5 bits

� 00000 represent 0� 11111 represent 0

� Range for positives: [0, -2n-1-1]� Range for negatives: [-(2n-1-1), 0]

ARCOS Computer Structure 45

Page 46: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Two’s complement

� Positive numbers: � In binary with n-1 bits

Magnitude(n-1 bits) n-1 n-2 00

Range : [0, 2n-1 -1]• Range : [0, 2n-1 -1]• Resolution: 1 unidad

Magnitude(n-1 bits) 0

i

2n

0i

ii

1n

0i

i x2x2V(X) ⋅=⋅= ∑∑−

=

=

Computer Structure 46ARCOS

Page 47: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Two’s complement

� Negative numbers: � Is base complemented. X< 0 is coded as 2n – X � First bit: is Not a signed bit, it belongs to the number value

n-1 n-2 0

Range: [-2n-1, -1]• Range: [-2n-1, -1]• Resolution: 1 unit

n-1 n-2 01

i

1

0i

i22 yV(X)n

n ⋅+−= ∑−

=

Computer Structure 47ARCOS

Page 48: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Two’s complement

� Ejemplo: Para n=5 ⇒ 00011 = + 3 � Example: with n=5, -3 = 11101

� 11101 ⇒ We obtain the one’s complement representation of

� If X > 0, two’s complement of X = X

� If X < 0, two’s complement of -X = (one’s complement of X) + 1

� 11101 ⇒ We obtain the one’s complement representation of11101, and adds 1 ⇒ = 00010 + 1 = 00011� i.e: -3

Range: [-2n-1, 2n-1-1]• Range: [-2n-1, 2n-1-1]• Resolution: 1 unit• There is only one zero (No ∃ -0)• Asymmetric range

Computer Structure 48ARCOS

Page 49: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Two’s complement

0000100010

1111111110 0 1

2-1

-2

. .

-311101

-411100

� 2N-1 positives

� 2N-1 negatives

� One zero

ARCOS Computer Structure 49

10000 0111110001

-15 -16 15

.

.

.

.

.

.

Page 50: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Two’s complement using 32 bits

0000 ... 0000 0000 0000 0000dos= 0(10

0000 ... 0000 0000 0000 0001dos= 1(10

0000 ... 0000 0000 0000 0010dos= 2(10

. . .0111 ... 1111 1111 1111 1101 = 2,147,483,6450111 ... 1111 1111 1111 1101dos= 2,147,483,645(10

0111 ... 1111 1111 1111 1110dos= 2,147,483,646(10

0111 ... 1111 1111 1111 1111dos= 2,147,483,647(10

1000 ... 0000 0000 0000 0000dos= –2,147,483,648(10

1000 ... 0000 0000 0000 0001dos= –2,147,483,647(10

1000 ... 0000 0000 0000 0010dos= –2,147,483,646(10

. . . 1111 ... 1111 1111 1111 1101dos= –3(10

1111 ... 1111 1111 1111 1110dos= –2(10

1111 ... 1111 1111 1111 1111dos= –1(10

ARCOS Computer Structure 50

Page 51: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Addition and subtraction

� With n = 5 bits

� X = 5

� In two’s complement = 00101

� Y = 7

Is two’s complement = 00111� Is two’s complement = 00111

� X + Y?

X = 00101

Y = 00111+

X+Y = 01100

� The value of 01100 in two’s complement is 12

ARCOS Computer Structure 51

Page 52: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Addition and subtraction

� With n = 5 bits

� X = -5

� In two’s complement = 11010 + 1 = 11011

� Y = -7

� In two’s complement = 11000 +1 = 11001

� X + Y?

-X = 11011

-Y = 11001+

-(X+Y) = 110100 discard the carry

� The result is 10100. The value is 01011 + 1 = 01100 = >- 12

ARCOS Computer Structure 52

Page 53: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Addition and subtraction

� With n = 5 bits

� X = 8

� In two’s complement = 01000

� Y = 9

In two’s complement = 01001� In two’s complement = 01001

� ¿X + Y?

X = 01000

Y = 01001+

X+Y = 10001

� A negative value is obtained ⇒ overflow

ARCOS Computer Structure 53

Page 54: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Addition and subtraction

� With n = 5 bits

� X = -8

� In two’s complement = 10111 + 1 = 11000

� Y = -9

� In two’s complement = 10110 +1 = 10111

� X + Y?

-X = 11000

-Y = 10111+

-(X+Y) = 101111 The carry is discarded

� The result 01111, is positive ⇒ overflow

ARCOS Computer Structure 54

Page 55: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Overflows in two’s complement

� Addition of two negative numbers ⇒ positive number� Addition of two positive numbers ⇒ negative number

ARCOS Computer Structure 55

Page 56: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Sign extension in two’s complement

� How to represent a number of n bits with m bits, being n < m?

� Example:� n = 4, m = 8� X = 0110 with 4 bits ⇒ X = 00000110 with 8 bits� X = 0110 with 4 bits ⇒ X = 00000110 with 8 bits� X = 1011 with 4 bits ⇒ X = 11111011 with 8 bits

ARCOS Computer Structure 56

Page 57: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Excess representation (2n-1-1)

Biased notation

� With n bits, 2n-1-1 is added to the value to be represented. The bias is 2n-1-1

0n bits

n-1

1−n

1)(2 - 2 V(X) 1ni

1

0i

i −⋅= −−

=∑ xn

Range: [-2n-1 +1, 2n -1]• Range: [-2n-1 +1, 2n -1]• Resolution: 1 unit• Only one zero

Computer Structure 57ARCOS

Page 58: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Comparative example(3 bits)

Decimal Binary Sign-magnitude One’s complement Two’s complement Excess-3

+7 111 N.D. N.D. N.D. N.D.

+6 110 N.D. N.D. N.D. N.D.

+5 101 N.D. N.D. N.D. N.D.

+4 100 N.D. N.D. N.D. 111

+3 011 011 011 011 110

+2 010 010 010 010 101

+1 001 001 001 001 100+1 001 001 001 001 100

+0 000 000 000 000 011

-0 N.D. 100 111 N.D. N.D.

-1 N.D. 101 110 111 010

-2 N.D. 110 101 110 001

-3 N.D. 111 100 101 000

-4 N.D. N.D. N.D. 100 N.D.

-5 N.D. N.D. N.D. N.D. N.D.

-6 N.D. N.D. N.D. N.D. N.D.

-7 N.D. N.D. N.D. N.D. N.D.

Computer Structure 58ARCOS

Page 59: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Example

� The value 110110 (6 bits)� What is the value?� In binary system= 25 + 24 + 22 + 21 = 52(10

� In sign-magnitude = - (24 + 22 + 21) = -21(10

� In one’s complement� Is complemented⇒ 001001 = 9(10� Is complemented⇒ 001001 = 9(10

� The value is -9(10

� In two’s complement� Is complemented ⇒ 001001 = 9. Add1 = 001010 = 10� The value is -10(10

� Excess-31 (26-1 -1 = 31)� Value of 110110(2 = 52(10

� Value stored= 52 – 31 = 21(10

ARCOS Computer Structure 59

Page 60: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Example

� Represent the following numbers

1. -17 is sign-magnitude with 6 bits2. +16 in two’s complement with 5 bits

-16 in two’s complement with 5 bits3. -16 in two’s complement with 5 bits4. +15 in one’s complement with 6 bits

ARCOS Computer Structure 60

Page 61: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Solution

1. 110001

2. With 5 bits we can not represent -17 :[-25-1 , 25-1-1] = [-16 , 15]

3. 100003. 10000

4. 001111

ARCOS Computer Structure 61

Page 62: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Example

� Using 5 bits, compute the followingg additions in one’s complement

a) 4 +12b) 4 -12c) –4 -12c) –4 -12

ARCOS Computer Structure 62

Page 63: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Solution

� Using 5 bits in one’s complement:a) 4 +12

0010001100----------------10000 ⇒ -15 ⇒ overflow

ARCOS Computer Structure 63

Page 64: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Solution

� Using 5 bits in one’s complement:b) 4 - 12

0010010011----------------10111⇒ -8

ARCOS Computer Structure 64

Page 65: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Solution

� Using 5 bits in one’s complement:c) -4 - 12

1101110011----------------

101110 ⇒ 6 bits are needed ⇒ overflow

ARCOS Computer Structure 65

Page 66: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Review

� Using N bits, we can represent:� 2N different codes� Numbers without sign:

0 a 2N - 1for N=32, 2N–1 = 4.294.967.295for N=32, 2 –1 = 4.294.967.295

� Signed-numbers in two’s complement:-2(N-1) a 2(N-1) - 1

for N=32, 2(N-1) = 2.147.483.648

ARCOS Computer Structure 66

Page 67: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Other things to represent

� How to represent?

� Very large numbers: 30.556.926.000(10

� Very small numbers: 0.0000000000529177(10(10

� Fractional numbers: 1,58567

� Real numbers

ARCOS Computer Structure 67

Page 68: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Fractional values in binary

� Example with 6 bits:

xx.yyyy21

20 2-1 2-2 2-3 2-4

ARCOS Computer Structure 68

2 20 2-1 2-2 2-3 2-4

� 10,1010(2 = 1x21 + 1x2-1 + 1x2-3 = 2.62510� Using this point, the range is:

� 0 a 3.9375 (almost 4)

Page 69: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Fractional powers of 2

0 1.0 1

1 0.5 1/2

2 0.251/4

3 0.125 1/8

4 0.0625 1/16

i 2-i

ARCOS Computer Structure 69

4 0.0625 1/16

5 0.03125 1/32

6 0.015625

7 0.0078125

8 0.00390625

9 0.001953125

10 0.0009765625

Page 70: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Floating-point numbers

� Each number has an exponent

� Scientific notation (in decimal) → normalized form, only one digit different to 0 to left of decimal point

ARCOS Computer Structure 70

6.0210 x 1023

radix (base)decimal point

mantissaexponent

Page 71: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Scientific notation in binary

1.0(2 x 2-1

radix (base)binary point

exponentmantissa

� Normalized form: One 1(only one digit) to the left of binary point

� Normalized: 1.0001 x 2-9

� Not normalized: 0.0011 x 2-8, 10.0 x 2-10

ARCOS Computer Structure 71

Page 72: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

IEEE 754 Floating Point Standard

� Standard used to store floating point values in computers

Characteristics

Sign

bit

ExponentMantisa (Significand)

� Characteristics� Exponent: Excess-k, with k=2n-1 -1 (being n the number of bits used

for the exponent)� Mantissa: sign-magnitude, normalized with implicit bit, similar to:

M = 1,xx…

Computer Structure 72ARCOS

Page 73: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

IEEE 754 Floating Point Standard

Format Bits Base Mantissa Exponent Excess-k

Single 32 2 23 bits 8 bits 127

Double 64 2 52 bits 11 bits 1023

ARCOS Computer Structure 73

Double 64 2 52 bits 11 bits 1023

Page 74: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Normalized numbers

� In this standard, numbers must be normalized:

� 1,bbbbbbb × 2e� mantissa: 1,bbbbbb (being b = 0, 1)� 2 is the base of the exponent� e is the exponent

Computer Structure 74ARCOS

Page 75: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Normalization

� Normalization: we need to normalize the mantissa. The exponent is adjusted to obtain one 1 in the most significant bit of the number� Example: 1.111001x 23 (already normalized)� Example: 1111.101 x 23 Not normalized, we move the decimal point

to left� 1111,101 x 23 = 1,111101 x 26

� 1,111101 x 26 normalized

Computer Structure 75ARCOS

Page 76: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

IEEE Standard 754 (single precision)

S: sign (1 bit)

E: exponent (8 bits)

M: mantissa (23 bits)

S E M

319810bits

� The value is computed as:N = (-1)S ×××× 2 E-127 ×××× 1.M

� where:S = 0 for positive numbers, S =1 for negative numbers0 < E < 255 (E=0 y E=255 are exceptions)00000000000000000000000 ≤ M ≤ 11111111111111111111111

� Implicit bit: when the number is normalized, the most significant bit is not stored in M. In this way, we can get more precision

Computer Structure 76ARCOS

Page 77: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Example

� Represent 7.5 and 1.5 using the IEEE 754 format

Computer Structure 77ARCOS

Page 78: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Solution

7.5 = 111.1 × 20 = 1.111 × 22

1.5 = 1.1 × 20

Computer Structure 78ARCOS

Page 79: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Solución

7.5 = 111.1 × 20 = 1.111 × 22

Sign = 0 (positive)Exponent = 2 -> exponent to store = 2 + 127= 129 = 10000001Mantissa = 1.111 -> mantissa to store = 1110000 … 0000Mantissa = 1.111 -> mantissa to store = 1110000 … 0000

1.5 = 1.1 × 20Sign = 0 (positive)Exponent = 0 -> exponent to store = 0 + 127= 127 = 01111111Mantissa = 1.1 -> mantissa to store = 1000000 … 0000

Computer Structure 79ARCOS

Page 80: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Solution

7.5 → 0 10000001 11100000000000000000000

7.5 = 111.1 × 20 = 1.111 × 22

1.5 → 0 01111111 10000000000000000000000

1.5 = 1.1 × 20

Computer Structure 80ARCOS

Page 81: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

IEEE Standard 754 (single precision)

� Special cases:

Exponent Mantissa Special value

0 (0000 0000) 0 +/- 0

× ×(s) × 0.mantissa × 2-126

0 (0000 0000) No cero Number not normalized

255 (1111 1111) No cero NaN (0/0, sqrt(-4), ….)

255 (1111 1111) 0 +/- infinite

1-254 Any Valor normal (no especial)

× ×(s) × 1.mantissa × 2exponent-127

Computer Structure 81ARCOS

Page 82: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

IEEE Standard 754 (single precision)

� Example:

a) Calculate the value of this number0 10000011 11000000000000000000000 represented in IEEE Standard 754 of single precision

a) Sign bit: 0 ⇒ positive number

b) Exponent: 100000112 = 13110 ⇒ E - 127 = 131 - 127 = 4

c) Mantissa stored: 11000000000000000000000

d) Mantisa: 1,1100 ⇒ 1 + 1 × 2-1 + 1 × 2-2 = 1,75

The value is 1.75 × 24 = 28

Computer Structure 82ARCOS

Page 83: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

IEEE Standard 754 (single precision)

Range

� Range for mantissa:

� Lowest number normalized:1.000000000000000000000002 × 2 1-127= 2-126

� Largest number normalized:1.111111111111111111111112 × 2 254-127= (2 - 2-23) × 2127

Trick:1.111111111111111111111112 = X

+ 0.000000000000000000000012 = 2-23------------------------------------------------------------------------------------------------------------------------------------------------------------

10.000000000000000000000002 = 2

X = 2 - 2-23

Computer Structure 83ARCOS

Page 84: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

IEEE Standard 754 (single precision)

Range

� Range for mantissa:

� Lowest number not normalized :0.000000000000000000000012 × 2-126 = 2-149

� Largest number not normalized: 0.111111111111111111111112 × 2-126 = (1 - 2-23) × 2-126

Trick:0.11111111111111111111111112 = X

+ 0.000000000000000000000012 = 2-23------------------------------------------------------------------------------------------------------------------------------------------------------------

1.000000000000000000000002 = 1

X = 1 - 2-23

Computer Structure 84ARCOS

Page 85: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

How many not normalized numbers

different to zero can be represented?

Exponent Mantissa Special value

× ×(s) × 0.mantissa × 2-126

ARCOS Computer Structure 85

0 (0000 0000) No cero Number not normalized

Page 86: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

How many not normalized numbers

different to zero can be represented?

Exponent Mantissa Special value

× ×(s) × 0.mantissa × 2-126

� Solution:� 23 bits for mantissa (different to 0)

223 -1

ARCOS Computer Structure 86

0 (0000 0000) No cero Number not normalized

Page 87: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Discrete representation

+-

ARCOS Computer Structure 87

0+-

� Density decreases toward infinity

Page 88: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Exercise

� Let f(1,2) = number of floats between 1 and 2� Let f(2,3) = number of floats between 2 and 3

� Which is larger f(1,2) o f(2,3)?

ARCOS Computer Structure 88

Page 89: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Exercise

� Let f(1,2) = number of floats between 1 and 2� Let f(2,3) = number of floats between 2 and 3

� Which is larger f(1,2) o f(2,3)?

� solution:� 1 = 1.0 × 20

� 2 = 1.0 × 21

� 3 = 1.1 × 21

� Between 1 and 2 there are 223 numbers� Between 2 and 3 there are 222 numbers

ARCOS Computer Structure 89

Page 90: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Curiosity

0.4 → 0 01111101 10011001100110011001101

3.9999998 e-13.9999998 e-1

0.1 → 0 01111011 10011001100110011001100

9.9999994 e-2

Computer Structure 90ARCOS

Page 91: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Example

� What is the binary and decimal value of the following number represented in the IEEE 754 standard?3FE00000

ARCOS Computer Structure 91

Page 92: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Solución

� Binary value:3 F E 0 0 0 0 0

0011 1111 1110 0000 0000 0000 0000 0000

� In decimal:0011 1111 1110 0000 0000 0000 0000 00000011 1111 1110 0000 0000 0000 0000 0000� Sign: 0� Exponent: 01111111 ⇒ 127-127 = 0� Mantissa: 1.11000000000000000000000 ⇒ 1+0.5+0.25 = 1.75

Then, the value is +1 × 1.75 × 20 = 1.75

ARCOS Computer Structure 92

Page 93: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Arithmetic on floating-point numbers

� Additions and subtractions:1) Check zero values.

2) Adjust mantissas (adjust exponents).

3) Add or subtract mantissas.

4) Normalize the result

Computer Structure 93ARCOS

Page 94: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Additions and subtractions

1. Check exponent: shift the smaller numberto right in order to make exponents equals

2. Add mantissasEnd

¿X = 0? ¿Y = 0?Z = X × Y

Yes

No

Z = Y

No

Yes

Z = X

ARCOS Computer Structure 94

¿Overflow

or underflow?Exception

3. Normalize result

4. Round

Normalized?

Si

Si

end

Page 95: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Arithmetic on floating-point numbers

� Examples

N1 = 0 10000001 11100000000000000000000 = +7.5

Computer Structure 95

N2 = 0 01111111 10000000000000000000000 = +1.5

ARCOS

Page 96: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Arithmetic on floating-point numbers

� Subtract the exponents:

E1 = 10000001 ⇒ Actual exponent = 129 -127 = 2

E2 = 01111111 - ⇒ Actual exponent = 127

00000010 = 2(10

� Shift the mantissa with the smaller exponent (1.M2) two bits

Computer Structure 96

� Shift the mantissa with the smaller exponent (1.M2) two bits to left (E1-E2) in order to make the exponents equals, including the implicit bit

� 1.M2=1.10000000000000000000000

⇒ 0.01100000000000000000000

ARCOS

Page 97: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Arithmetic on floating-point numbers

� Add mantissas: 1.M1, y 1.M2 :M1 = 1.11100000000000000000000M2 = 0.01100000000000000000000 +MS = 10.01000000000000000000000

� Result: 10.01 × 22

� Normalize the result: 1.001 x × 23 = 9(10

Computer Structure 97

� The final result is:Result= 0 10000010 00100000000000000000000 � 0 ⇒ + � e = 3 ⇒ E = 127 + 3 = 10000010� Mantissa = 1.00100000000000000000000, The mantissa stored is

00100000000000000000000

ARCOS

Page 98: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Arithmetic on floating-point numbers

� Multiplications and divisions:

1. Check zero values.

2. Add (subtract) exponents.

3. Multiply (divide) mantissas .

Computer Structure 98

4. Normalize.

5. Round.

ARCOS

Page 99: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Floatint-point

multiplication

Multiplication

¿X = 0?

Z 0

END

¿Y = 0?Addexponents

Subtract bias

Overflow

¿Exponentunderflow?

No

NoYes

Yes

Yes

YesYes

Exponentoverfllow?

underflow

Z = X × Y

Computer Structure •99

¿Exponentunderflow?

Normalize

Round END

No

Multiplymantissas

ARCOS

Page 100: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Floating-point

division

DIVIDE

¿X = 0?

Z 0

END

¿Y = 0?

Z ∞

Subtractexponents

Add bias

Exponentoverflow?

Exponentunderflow?

Underflow

No

No

No

Yes

YesYes

Yes Overflow

Z = X / Y

Computer Structure 100

Divide mantissas

Normalize

Round END

No

ARCOS

Page 101: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Example

� Multiply 7.5 y 1.5

Computer Structure 101ARCOS

Page 102: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Solution (1)

7.5 × 1.5 = (1.1112 × 22) × (1.12 × 20) = (1.1112 × 1,12) × 2(2+0)

= (10.11012) × 22= (10.11012) × 2= (1.011012) × 23

= 11.25

Computer Structure 102ARCOS

Page 103: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Solution (2)

7.5 → 0 10000001 1.11100000000000000000000

1.5 → 0 01111111 1.10000000000000000000000×

Computer Structure 103ARCOS

Include the implicit bit

Page 104: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Solution (3)

7.5 → 0 10000001 1. 11100000000000000000000

1.5 → 0 01111111 1. 10000000000000000000000×

� Add the exponents and multiply mantissas

0 100000000 10.11010000000000000000000

Computer Structure 104ARCOS

×+

Exponent with 9 bits

Page 105: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Solution(4)

7.5 → 0 10000001 1. 11100000000000000000000

1.5 → 0 01111111 1. 10000000000000000000000×

� Subtract the bias (127)

0 100000000 10.11010000000000000000000

Computer Structure 105ARCOS

×

0 10000001 10.11010000000000000000000

+

- 01111111

Page 106: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Solution(5)

7.5 → 0 10000001 1.11100000000000000000000

1.5 → 0 01111111 1.10000000000000000000000×

� Normalize

11.25 0 10000010 1.011010000000000000000000

Computer Structure 106ARCOS

Page 107: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Solution(6)

11,25 0 10000010 011010000000000000000000

� Eliminate the implicit bit

Computer Structure 107ARCOS

Page 108: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Rounding and guard bits

� The floating-point hardware includes two extra bits (guard bits) before to make the arithmetic operations

� After operations, these bits are eliminated: rounding

� Rounding appears when:� A double precision value is converted to single precision� A double precision value is converted to single precision� A floating-point number is converted to integer

Computer Structure 108ARCOS

Page 109: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Guard bits

� Add 2.56 x 100 y 2.34x 102 assuming we have only 3 decimal digits for mantissas:

2.56 x 100 0.02 x 102

ARCOS Computer Structure 109

2.56 x 10

+ 2.34x 102

0.02 x 102

+ 2.34x 102

2,36 x 102Adjust exponent

Page 110: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Guard bits

� Add 2.56 x 100 y 2.34x 102 assuming we have only 3 decimal digits for mantissas using two guard digits

2.56 x 100 2.5600 x100

ARCOS Computer Structure 110

2.56 x 10

+ 2.34x 102

2.5600 x10

+ 2.3400x 102

Include twoextra guarddigits

Page 111: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Guard bits

� Add 2.56 x 100 y 2.34x 102 assuming we have only 3 decimal digits for mantissas using two guard digits

2.5600 x100 0.0256 x102

ARCOS Computer Structure 111

2.5600 x10

+ 2.3400x 102

Adjust theexponent

0.0256 x102

+ 2.3400x 102

2.3656 x 102

Round

2.37 x 102

Page 112: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Rounding in IEEE Standard 754

� Round to + ∞�Round up: 2.001 → 3, -2.001 → -2

�Round to -∞� Round down 1.999 → 1, -1.999 → -2

�Truncate� discard last bits� discard last bits

�Round to nearest even�2.4 → 2, �2.6 → 3, �2.5 → 2,� 3.5 → 4

Computer Structure 112ARCOS

Page 113: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Floating-point is not associative

� Floating-point is not associative� x = – 1.5 × 1038, y = 1.5 × 1038, y z = 1.0

� x + (y + z) = –1.5× 1038 + (1.5× 1038 + 1.0)= –1.5× 1038 + (1.5× 1038) = 0.0

× ×= –1.5× 10 + (1.5× 10 ) = 0.0

� (x + y) + z = (–1.5× 1038 + 1.5× 1038) + 1.0= (0.0) + 1.0 = 1.0

� Floating-point operations are not associatives� Results are approximated� 1.5 × 1038 is so much larger than 1.0 � 1.5 × 1038 + 1.0 in floating point representation is still 1.5 ×

1038

ARCOS 113Computer Structure

Page 114: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

int →→→→ float →→→→ int

if (i == (int)((float) i)) {

printf(“true”);

}

� Will not always print “true”� Most large values of integers don’t have exact floating

point representations!� What about double ?

ARCOS 114Computer Structure

Page 115: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

Example

� The number 133000405 in binary is:� 111111011010110110011010101 (27 bits)

� 111111011010110110011010101 × 20

� When is normalized:� 1, 11111011010110110011010101 × 226� 1, 11111011010110110011010101 × 2� S = 0 (positive)� e = 26 → E = 26 + 127 = 153� M = 11111011010110110011010 (last 3 bits are lost)

� The normalized number stored is:� 1, 11111011010110110011010 × 226 =� 111111011010110110011010 × 23 = 133000400

ARCOS Computer Structure 115

Page 116: Computer Structureocw.uc3m.es/.../computer-structure/unit2.pdf · 2013-01-24 · Computer Structure Unit 2. Data representation Departamento de Informática Grupo de Arquitectura

float →→→→ int →→→→ float

� Will not always print “true”

if (f == (float)((int) f)) {

printf(“true”);

}

� Will not always print “true”� Small floating point numbers (<1) don’t have integer

representations� For other numbers, rounding errors

ARCOS Computer Structure 116