Docsity

Binary Number Representation and Floating Point Numbers, Study notes of Computer Architecture and Organization

Partial preview of the text

Download Binary Number Representation and Floating Point Numbers and more Study notes Computer Architecture and Organization in PDF only on Docsity!

Chapter

Chapter

Bits, Data Types & OperationsBits,^

Data^

Types

&^ Operations

z^ Integer

Representation z^ Floating

‐point Representation z^ Other

data^ types

Why

do

Computers

use

Base

Why

do

Computers

use

Base

z^ Base

10 Number

Representation

-^ Natural

representation

for^ human

transactions

H^ d^

I^ l^

El^

i^ ll^

3.5 = 3×

0 + 5×

z^ H

ard^ to

Implement

Electronically

-^ Hard

to^ store z ENIAC^ (First

electronic

computer)

used^10

vacuum

tubes^ /

digit

-^ Hard

to^ transmitN^ d hi h

i i

t^

d^10

i^ l l^

l^ i^

l^ i

z^ Need

high^ precision

to^ encode

10 signal

levels^ on^ single

wire

-^ Messy

to^ implement

digital

logic^ functions

z^ Addition,

multiplication,

etc.

z^ Binary

RepresentationSpecifically the devices that make up a computer are switches that can be– Specifically,

the^ devices

that^ make

up^ a^ computer

are^ switches

that^ can

be

on^ or^ off,

i.e.^ at

high^ or

low^ voltage.

-^ Easy

to^ store

with^ bistable elements

-^ Reliably

transmitted

on^ noisy

and^ inaccurate

wires

z^ Examples

1

0

1

z^ Examples

-^ Represent

3.5^10

as^ 11.

2

-^ Represent

15213

as^1110110110110110

2

-^ Represent

1.20^10

as^ 1.0011001100110011[0011]…

2

z^ Binary floating point number cannot exactly represent $1 20

3.5 = 1×

1 + 1×

0 + 1×

-1^ +…

Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering

CEG 320/520Comp. Org. & Assembly

z^ Binary

floating

point^

number

cannot

exactly

represent

$1.

Unsigned

Binary

Integers

Unsigned

Binary

Integers

Y = “abc” = a.

2 + b.

1 + c.

3-bits

5-bits

8-bits

(where the digits a, b, c can each take on the values of 0 or 1 only)^ N^ = number of bits

Range is: 0

≤^ i < 2

N^ – 1

Umin = 0

Umin

Umax = 2

N^ – 1

Problem:

Problem: • How do we represent^ negative

numbers?

Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering

CEG 320/520Comp. Org. & Assembly

g

Signed

Magnitude

Signed

Magnitude

z^ Leading

bit^ is

the^ sign bit

-^

-^

-^

Range is: -

N-1^ + 1 < i < 2

N-1^ – 1

Y = “abc” = (-1)

a^ (b.

1 + c.

-^

-^

g

Smin = -

N-1^ + 1

Smax = 2

N-^

+^

+^

+^

Smax

N 1 = 2

Problems:

+^

+^

+^

• How do we do addition/subtraction?• We have two numbers for zero (+/-)!

Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering

+4^ CEG 320/520Comp. Org. & Assembly

Manipulating

Binary

numbers

Manipulating

Binary

numbers

‐‐^11

z^ Binary

to^ Decimal

conversion

&^ vice

‐versa

  • A^4 bit

binary

number

A^ =^ a^3

a^ a^ a^210

corresponds

to:

(^3) a x (^23) +^ a^ x^2

2 2 +^ a^1

(^1) x 2 +^

(^0) a x (^20) =^ a^ x^3

8 +^ a^2 x^4 +^ a

x^2 +^1

a^ x^10

(where

a^ =^0 i^

or^1 only)

  • A^ decimal

number

can^ be

broken

down

by^ iteratively

determining

the^ highest

power

of

two^ that

“fits”^

in^ the^

number:

e.g.^ (13)

=> 10 e.g.^ (63)

=> 10 e.g.^ (0.63)

=> 10

  • In^ the

2’s^ complement

representation,

leading

zeros^

do^ not affect

the^ value

of^ a^ positive

binary

number,

and^ leading

ones^ do

not affect

the^ value

of^ a^ negative

number.

So:

01101 = 00001101 = 13

and^

11011 = 11111011 = 5

01101

=^00001101

=^13

and^11011

=^11111011

=^ ‐^5

00001101

x0D

11111011

xFB

Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering

CEG 320/520Comp. Org. & Assembly

00001000

=>^8 (as

expected!)

x

Manipulating

Binary

numbers

Manipulating

Binary

numbers

‐‐^1010

z^ Overflow– If

we^ add

the^ two

(2’s^ complement)

4 bit^

numbers

representing

7 and

5 we

get^ :

=>^ +

=>^ +

=>^ ‐^4

(in^4

bit^ 2’s

comp.)

z^ We^ get^ ‐4,

not^ +

as^ we

would

expect

!!

z^ We have

overflowed

the range of 4 bit 2’s comp ( 8 to +7) so the result is invalid

z^ We^ have^ overflowed

the^ range

of^4 bit

2 s^ comp

.^ (‐^8 to

+7),^ so

the^ result

is^ invalid

.

z^ Note

that^ if

we^ add

16 to^

this^ result

we^ get

back^16

‐^4 =^12

  • this^ is

like^ “stepping

up”^ to

5 bit^ 2’s

complement

representation

I^

l if h

f^

i i^

b^

d^

i^

l

  • In^ general,

if^ the

sum^ of^ two

positive

numbers

produces

a^ negative

result,

or

vice^ versa,

an^ overflow

has^ occurred,

and^ the

result

is^ invalid

in^ that

representation. Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering

CEG 320/520Comp. Org. & Assembly

C++

Integer

Puzzles

C++

Integer

Puzzles

  • Assume

machine

with^

32 bit

word

size,^

two’s

complement

integers

For each of the following C++ expressions either– For^ each

of^ the

following

C++^ expressions

,^ either

z^ Argue

that^ is

true^ for

all^ argument

values

z^ Give

example

where

not^ true

-^ x * x >= 0 •^ ux >= 0 •^ ux

>^ -

Initialization

ux >

-^ x < 0

⇒^ (2x) < 0*

-^ x > y

⇒^ -x < -y

Initializationint x = foo();int y = bar();

-^ x > 0 && y > 0

⇒^

x + y > 0

-^ x >= 0

⇒^ -x <= 0

y unsigned ux = x;unsigned uy = y;

Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering

CEG 320/520Comp. Org. & Assembly

-^ x^

⇒^ - x

ProblemsProblems

z^ Patt &

Patel,

Chapter

p

-^ 2.4,

2.8,^

Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering

CEG 320/520Comp. Org. & Assembly

Real

numbers

Real

numbers

z^ Most

numbers

are^ not

integer!

z^ Range:– The

magnitude

of^ the

numbers

we^ can

represent

is^ determined

by^ how

many

bits^ we

use:^ z^ e.g.

with^32

bits^ the

largest

number

we^ can

represent

is^ about

+/‐^2 billion,

far^ too

small^ for

many^ purposes. z^ Precision:– The

exactness

with^ which

we^ can

specify

a^ number:

z^ e.g.^ a

32 bit^ number

gives^ us

31 bits

of^ precision,

or^ roughly

9 figure

precision

in^ decimal

representationrepresentation z^ Our

decimal

system

handles

non‐integer

real^ numbers

by^ adding

yet^ another

symbol

‐^ the decimal

point

(.)^ to make

a^ fixed

point

notation:

  • e.g.^

3,456.

=^ 3.

3 +^ 5.

2 +^ 4.

1 +^ 6.

0 +^ 7.

‐^1 +^ 8.

‐^2

g^ , z^ The

floating

point

,^ or^ scientific,

notation

allows

us^ to

represent

very^ large^

and

very^ small

numbers

(integer

or^ real),

with^

as^ much

or^ as

little^

precision

as

needed.

Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering

CEG 320/520Comp. Org. & Assembly

needed.

Real

numbers

in^

a^ fixed

space

Real

numbers

in^

a^ fixed

space

z^ What

if^ you

only

have

8 digits

to^ represent

a^ real

number?

z^ Do

you^

prefer…

...^ a^ low

‐precision

number

with

a^ large

range?

…^ or^

a^ high

‐precision

number

with

a^ smaller

range?

Wouldn’t

it^ be^

nice^ if

we^ could

“float”

the^ decimal

Wouldn’t

it^ be^

nice^ if

we^ could

“float”

the^ decimal

f^

f f^

f

point to^ allow

for^ either

case?

point to^ allow

for^ either

case?

Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering

CEG 320/520Comp. Org. & Assembly

Real

numbers

in^

binary

Real

numbers

in^

binary

z^ We

mimic

the^ decimal

floating

point

notation

to^ create

a^ “hybrid”

binary

floating point number:floating

point

number:

  • We^

first^ use

a^ “binary

point”

to^ separate

whole

numbers

from fractional

numbers

to^ make

a^ fixed

point

notation:

z^ e.g. 25.

=^ 1.2 10 4 + 1.

3 + 1.

1 + 1.

‐^1 + 1.

‐^2 = 11001.

2

e.g.^ 25.

1.2 10

2

‐^1 (2=^ 0.

and^2 ‐^2 =^ 0.25,

etc.)

  • We^

then^ “float”

the^ binary

point:

z^ 00011001.

=>^ 1.

(^4) x 2

mantissa

=^ 1.1001110,

exponent

=^4

  • Now

we^ have

to^ express

this^ without

the^ extra

symbols

(^ x,^ 2,

.^ )

p^

y^

(^ ,^ ,

z^ by^ convention,

we^ divide

the^ available

bits^ into

three fields:

sign,^ mantissa,

exponent

Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering

CEG 320/520Comp. Org. & Assembly

IEEEIEEE

‐‐^754754

fpfp numbers

numbers

bit

“float”

bit

“float”

s^ biased exp.

fraction

8 bits

23 bits

32 bits: z^ Sign: 1 bit

N = (-1)

s^ x 1.fraction x 2

( biased exp.

s^ biased

exp.

fraction

z^ Sign:

1 bit

z^ Mantissa:

23 bits

  • We

“normalize”

the^ mantissa

by^ dropping

the^ leading

1 and

recording

only^ its

f^ ti l^

t (^ h ?) fractional

part^ (why?)

z^ Exponent:

8 bits

  • In^ order

to^ handle

both^

+^ and

‐^ exponents,

we^ add

127 to

the^ actual

t t^

t^ “bi

d^

t” (thi

id^

l^

hi^

d^ !)

exponent

to^ create

a^ “biased

exponent”

(this^

provides

a^ lexographic

order!)

‐^127 z 2 =>^ biased

exponent

=^0000

0000

(=^ 0)

(^0) z 2 => biased

exponent

=^0111

1111

(=^ 127)

+127 z 2 => biased exponent = 1111 1110 (= 254)

Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering

CEG 320/520Comp. Org. & Assembly

z^2

^ biased

exponent

1111

1110

(^ 254)

IEEEIEEE

‐‐^754754

fpfp numbers

numbers

– – 64^64

‐‐bit

“double”

bit

“double”

z^ Double

precision

(64^ bit)

floating

point

s^ biased exp.

fraction

11 bits

52 bits

64 bits:

N = (-1)

s^ x 1.fraction x 2

(biased exp. – 1023)

R^

& P

i i

z^ R

ange & Precision: Š^ 32 bit:^ ƒ^ mantissa of 23 bits + 1 => approx. 7 digits decimal^ ƒ^2

+/-^ => approx 10

+/-

+/ 127ƒ 2 => approx. 10

+/ 38

Š^ 64 bit:^ ƒ^

mantissa of 52 bits + 1 => approx. 15 digits decimal+/ 1023 2

Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering

CEG 320/520Comp. Org. & Assembly

=> approx. 10

+/-

Floating

point

in^

C/C++

Floating

point

in^

C/C++

z^ C

Guarantees

Two

Levels

float

single

precision

double

double

precision

z^ Conversions

  • Casting

between

int,

float

,^ and^

double

changes

numeric

values

g^

,^

,^

g

-^ Double

or^ float

to^ int

z^ Truncates

fractional

part

z^ Like

rounding

toward

zero

z^ Not

defined

when

out^ of

range

-^ Generally

saturates

to^ TMin

or^ TMax

-^ int

to^ double E^

i^

l^

i^ h^

≤^ 53 bi

d

i

z^ Exact

conversion,

as^ long

as^ int^

has^ ≤^ 53 bit^

word^ size

-^ int

to^ float z Will^ round

according

to^ rounding

mode

Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering

CEG 320/520Comp. Org. & Assembly