35
Topic 2: Number Systems Topic 2: Number Systems 1 What are the basic digital logic operations? What are the basic digital logic operations? What are number systems? What are number systems? How do we perform arithmetic in binary? How do we perform arithmetic in binary? Digital Logic Digital Logic Digital Logic considers two values: Digital Logic considers two values: – True True – False False 2 • Representation Representation – True may be abbreviated with T or 1 True may be abbreviated with T or 1 – False may be abbreviated with F or 0 False may be abbreviated with F or 0

Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

Topic 2: N

umber S

ystems

Topic 2: N

umber S

ystems

1

What are the basic digital logic operations?

What are the basic digital logic operations?

What are num

ber systems?

What are num

ber systems?

How

do we perform

arithmetic in binary?

How

do we perform

arithmetic in binary?

Digital Logic

Digital Logic

••D

igital Logic considers two values:

Digital Logic considers tw

o values:––

True

True

––F

alseF

alse

2

••R

epresentationR

epresentation––

True m

ay be abbreviated with T

or 1T

rue may be abbreviated w

ith T or 1

––F

alse may be abbreviated w

ith F or 0

False m

ay be abbreviated with F

or 0

Page 2: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

Digital Logic

Digital Logic

••T

ruth tables describe the behavior of T

ruth tables describe the behavior of logical operatorslogical operators

Input(s)O

utputA

not A

3

••T

he not operator flips the value of its inputT

he not operator flips the value of its input

InputV

aluesO

utputV

alues01

Digital Logic

Digital Logic

••A

nd Operator

And O

perator––

Takes tw

o inputsT

akes two inputs

––P

roduces one outputP

roduces one output––

Output is T

rue if and only if both inputs are O

utput is True if and only if both inputs are

4

––O

utput is True if and only if both inputs are

Output is T

rue if and only if both inputs are truetrue

A B

0 00 11 01 1

A and B

Page 3: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

Digital Logic

Digital Logic

••O

r Operator

Or O

perator––

Takes tw

o inputsT

akes two inputs

––P

roduces one outputP

roduces one output––

Output is T

rue if one input is true (or both O

utput is True if one input is true (or both

5

––O

utput is True if one input is true (or both

Output is T

rue if one input is true (or both inputs are true)inputs are true)A

B

0 00 11 01 1

A or B

Digital Logic

Digital Logic

••E

xclusive Or O

peratorE

xclusive Or O

perator––

Takes tw

o inputsT

akes two inputs

––P

roduces one outputP

roduces one output––

Output is T

rue if exactly one input is trueO

utput is True if exactly one input is true

6

––O

utput is True if exactly one input is true

Output is T

rue if exactly one input is true

A B

0 00 11 01 1

A xor B

Page 4: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

Digital Logic

Digital Logic

••W

hen is not(A and B

) true?W

hen is not(A and B

) true?

A B

0 0

A and B

not (A and B

)

7

••W

e call this operation NA

ND

We call this operation N

AN

D

0 00 11 01 1D

igital LogicD

igital Logic

••W

hen is not(A or B

) true?W

hen is not(A or B

) true?

A B

0 0

A or B

not (A or B

)

8

••W

e call this operation NO

RW

e call this operation NO

R

0 00 11 01 1

Page 5: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

Digital Logic

Digital Logic

••E

xample:

Exam

ple:––

Construct a truth table for A

and (B or not C

): C

onstruct a truth table for A and (B

or not C):

9

Digital Logic

Digital Logic

••D

igital logic is the basis for computation in

Digital logic is the basis for com

putation in m

odern computers

modern com

puters––

Circuits can im

plement logical operations

Circuits can im

plement logical operations

––A

rithmetic operations can be built up from

A

rithmetic operations can be built up from

10

––A

rithmetic operations can be built up from

A

rithmetic operations can be built up from

logical operationslogical operations

––M

emory can be constructed by including

Mem

ory can be constructed by including feedback loops in the circuitsfeedback loops in the circuits

Page 6: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

Logic Gates

Logic Gates

••A

nd:A

nd:

••O

r:O

r:

11

••X

orX

or::

••N

andN

and::

••N

or:N

or:

Logic Gates

Logic Gates

••D

raw the logic gates to com

pute A and (B

D

raw the logic gates to com

pute A and (B

or not C

): or not C

):

12

Page 7: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

Logic Gates

Logic Gates

13

What is D

ecimal?

What is D

ecimal?

••T

hink about the way w

e normally count?

Think about the w

ay we norm

ally count?––

How

many unique sym

bols are there?H

ow m

any unique symbols are there?

14

Page 8: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

Representing Larger N

umbers

Representing Larger N

umbers

••W

e only have 10 distinct symbols

We only have 10 distinct sym

bols••

Positional representation allow

s us to P

ositional representation allows us to

represent larger numbers

represent larger numbers

––W

hat does 24 really mean?

What does 24 really m

ean?

15

––W

hat does 24 really mean?

What does 24 really m

ean?

––W

hat does 3709 really mean?

What does 3709 really m

ean?

What is B

inary?W

hat is Binary?

••A

number system

with only tw

o distinct A

number system

with only tw

o distinct sym

bolssym

bols––

Norm

ally denoted by 0 and 1N

ormally denoted by 0 and 1

––U

sed extensively in digital electronicsU

sed extensively in digital electronics

16

––U

sed extensively in digital electronicsU

sed extensively in digital electronics––

Uses the sam

e positional rules as base 10U

ses the same positional rules as base 10

••W

hat does 10110 mean in base 10?

What does 10110 m

ean in base 10?

••W

hat does 10110 mean in base 2?

What does 10110 m

ean in base 2?

Page 9: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

What is B

inary?W

hat is Binary?

••M

ore examples:

More exam

ples:––

Convert 1111

Convert 1111

22to base 10to base 10

17

––C

onvert 100010C

onvert 10001022

to base 10to base 10

––C

onvert 0C

onvert 022

to base 10to base 10

Converting B

etween B

asesC

onverting Betw

een Bases

••H

ow do w

e convert from B

inary to H

ow do w

e convert from B

inary to D

ecimal?

Decim

al?––

Use positional representation

Use positional representation

••H

ow do w

e convert from D

ecimal to

How

do we convert from

Decim

al to

18

••H

ow do w

e convert from D

ecimal to

How

do we convert from

Decim

al to B

inary?B

inary?

Page 10: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

The D

ivision Method

The D

ivision Method

••A

llows us to convert from

Decim

al to A

llows us to convert from

Decim

al to B

inaryB

inary––

Let N represent the num

ber to convertLet N

represent the number to convert

––S

et Q equal to N

Set Q

equal to N

19

––S

et Q equal to N

Set Q

equal to N––

Repeat

Repeat

••D

ivide Q by 2, recording the Q

uotient, Q, and the

Divide Q

by 2, recording the Quotient, Q

, and the rem

ainder, Rrem

ainder, R

––U

ntil Q is 0

Until Q

is 0––

Read the rem

ainders from bottom

to topR

ead the remainders from

bottom to top

The D

ivision Method

The D

ivision Method

••E

xample:

Exam

ple:––

Convert 12

Convert 12

1010to base 2 using the division to base 2 using the division

method

method

20

Page 11: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

The D

ivision Method

The D

ivision Method

••E

xample:

Exam

ple:––

Convert 191

Convert 191

1010to binary using the division to binary using the division

method

method

21

Other B

asesO

ther Bases

••A

number system

can have any baseA

number system

can have any base––

Decim

al: Base 10

Decim

al: Base 10

––B

inary: Base 2

Binary: B

ase 2––

Octal: B

ase 8O

ctal: Base 8

22

––O

ctal: Base 8

Octal: B

ase 8––

Hexadecim

al: Base 16

Hexadecim

al: Base 16

––V

igesimal

Vigesim

al: Base 20

: Base 20

––O

r any other number w

e choose…O

r any other number w

e choose…

Page 12: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

Hexadecim

alH

exadecimal

••B

ase 16 B

ase 16 ––com

monly used by com

puter com

monly used by com

puter scientistsscientists––

Requires 16 distinct sym

bolsR

equires 16 distinct symbols

––F

irst 10 are 0 .. 9F

irst 10 are 0 .. 9

23

––F

irst 10 are 0 .. 9F

irst 10 are 0 .. 9––

Rem

ainder are letters A, B

, C, D

, E, F

Rem

ainder are letters A, B

, C, D

, E, F

––C

onvert 10C

onvert 101616

to base 10:to base 10:

––C

onvert 0xD5E

to base 10:C

onvert 0xD5E

to base 10:

Hexadecim

alH

exadecimal

••E

xample:

Exam

ple:––

Convert 222

Convert 222

1010to H

exadecimal

to Hexadecim

al

24

Page 13: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

Arbitrary B

ase Conversions

Arbitrary B

ase Conversions

••H

ow do w

e convert NH

ow do w

e convert NAA

to base B?

to base B?

––C

onvert NC

onvert NAA

to base 10to base 10

––C

onvert from base 10 to base B

Convert from

base 10 to base B––

Exam

ple: Convert 452

Exam

ple: Convert 452

to base 12to base 12

25

––E

xample: C

onvert 452E

xample: C

onvert 45277

to base 12to base 12

Arbitrary B

ase Conversions

Arbitrary B

ase Conversions

••A

nother Exam

ple:A

nother Exam

ple:––

Convert 0xF

F to base 6

Convert 0xF

F to base 6

26

Page 14: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

Base 2

Base 2

001110101111100100101101110110111111

Base 8

Base 8

0011223344556677

Base 10

Base 10

0011223344556677

Base 16

Base 16

0011223344556677

27

10001000100110011010101010111011110011001101110111101110111111111000010000

101011111212131314141515161617172020

88991010111112121313141415151616

8899AABBCCDDEEFF1010

Grouping

Grouping

••F

aster arbitrary base conversions are F

aster arbitrary base conversions are possible in som

e casespossible in som

e cases––

Let A and B

represented the basesLet A

and B represented the bases

––If A

=

If A =

BBnn

or B =

Aor B

= A

nnfor som

e positive integer, n, for som

e positive integer, n, 28

––If A

=

If A =

BBnn

or B =

Aor B

= A

nnfor som

e positive integer, n, for som

e positive integer, n, then w

e can convert using groups of n digitsthen w

e can convert using groups of n digits••

Converting from

a larger base to a smaller base

Converting from

a larger base to a smaller base

means that w

e generate n digits of output for each m

eans that we generate n digits of output for each

digit of inputdigit of input

••C

onverting from a sm

aller base to a larger base C

onverting from a sm

aller base to a larger base m

eans that we use n digits of input to create on

means that w

e use n digits of input to create on digit of outputdigit of output

Page 15: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

Grouping

Grouping

••E

xample:

Exam

ple:––

Convert 315

Convert 315

88to B

inaryto B

inary

29

Grouping

Grouping

••E

xample:

Exam

ple:––

Convert 11302

Convert 11302

44to base 16to base 16

30

Page 16: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

Grouping

Grouping

••E

xample:

Exam

ple:––

Convert 354

Convert 354

88to H

exadecimal

to Hexadecim

al

31

Hexadecim

al Shorthand

Hexadecim

al Shorthand

••W

riting long sequences of 0 and 1 is W

riting long sequences of 0 and 1 is cum

bersome and error prone

cumbersom

e and error prone––

Com

puter Scientist frequently use

Com

puter Scientist frequently use

hexadecimal to represent sequences of bits

hexadecimal to represent sequences of bits

32

hexadecimal to represent sequences of bits

hexadecimal to represent sequences of bits

––M

emorize your hexadecim

al, binary and M

emorize your hexadecim

al, binary and decim

al conversions for 0 to 15decim

al conversions for 0 to 15

Page 17: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

Arbitrary B

ase Conversions

Arbitrary B

ase Conversions

••W

hy do computer scientists keep on

Why do com

puter scientists keep on confusing H

alloween and C

hristmas?

confusing Hallow

een and Christm

as?

33

Fractions

Fractions

••W

hat does 3.1415W

hat does 3.14151010

really mean?

really mean?

34

••W

hat does 101.101W

hat does 101.10122

really mean?

really mean?

Page 18: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

Conversions Involving F

ractionsC

onversions Involving Fractions

••C

onvert 4.125C

onvert 4.1251010

to Binary

to Binary

––S

eparate the integer and fractional portionsS

eparate the integer and fractional portions••

Use the division m

ethod on the integer portionU

se the division method on the integer portion

••U

se multiplication m

ethod on fractional portionU

se multiplication m

ethod on fractional portion

35

••U

se multiplication m

ethod on fractional portionU

se multiplication m

ethod on fractional portion

Conversions Involving F

ractionsC

onversions Involving Fractions

••C

onvert 3.1415C

onvert 3.14151010

to Hexadecim

al:to H

exadecimal:

36

Page 19: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

Num

ber System

sN

umber S

ystems

••B

ase 10 is natural because we learned it

Base 10 is natural because w

e learned it firstfirst––

Nothing special about it beyond that

Nothing special about it beyond that

––O

ffers same expressive pow

er as other basesO

ffers same expressive pow

er as other bases

37

––O

ffers same expressive pow

er as other basesO

ffers same expressive pow

er as other bases

••B

inary and bases that are powers of 2 are

Binary and bases that are pow

ers of 2 are frequently used in com

puter sciencefrequently used in com

puter science––

Digital electronics are inherently binary

Digital electronics are inherently binary

Representing N

umbers

Representing N

umbers

••C

omputers are inherently binary

Com

puters are inherently binary––

Everything is a 1 or a 0

Everything is a 1 or a 0

––S

ize Limits

Size Lim

its••

Com

puters don’t have infinite mem

oryC

omputers don’t have infinite m

emory

38

••C

omputers don’t have infinite m

emory

Com

puters don’t have infinite mem

ory••

Can only represent num

bers within a confined

Can only represent num

bers within a confined

rangerange

––N

umbers are represented in binary

Num

bers are represented in binary••

Natural for positive num

bersN

atural for positive numbers

••H

ow do w

e represent a negative number?

How

do we represent a negative num

ber?

Page 20: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

Size Lim

itsS

ize Limits

••C

omm

on sizes for numbers in a com

puterC

omm

on sizes for numbers in a com

puter––

8 bits (referred to as a byte)8 bits (referred to as a byte)

––16 bits (often referred to as a half16 bits (often referred to as a half--w

ord)w

ord)

39

––16 bits (often referred to as a half16 bits (often referred to as a half--w

ord)w

ord)

––32 bits (often referred to as a w

ord)32 bits (often referred to as a w

ord)

––64 bits (often referred to as a double w

ord)64 bits (often referred to as a double w

ord)

Negative N

umbers

Negative N

umbers

••E

verything is a 1 or a 0E

verything is a 1 or a 0––

Can’t use a negative sign directly

Can’t use a negative sign directly

––M

ust encode as either a 1 or a 0M

ust encode as either a 1 or a 0––

Several encoding choices are available

Several encoding choices are available

40

––S

everal encoding choices are availableS

everal encoding choices are available••

Advantages and disadvantages of each encoding

Advantages and disadvantages of each encoding

Page 21: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

Signed M

agnitudeS

igned Magnitude

••S

igned Magnitude

Signed M

agnitude––

Decide that the left m

ost bit in a unit D

ecide that the left most bit in a unit

represents the signrepresents the sign

––D

ecide that 0 represents positive and 1 D

ecide that 0 represents positive and 1

41

––D

ecide that 0 represents positive and 1 D

ecide that 0 represents positive and 1 represents negativerepresents negative

––R

emainder of the bits represent the

Rem

ainder of the bits represent the m

agnitude in positional representationm

agnitude in positional representation

Signed M

agnitudeS

igned Magnitude

••H

ow do w

e represent 100H

ow do w

e represent 1001010

as a byte?as a byte?

42

••H

ow do w

e represent H

ow do w

e represent --100100

1010as a byte?as a byte?

Page 22: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

Signed M

agnitudeS

igned Magnitude

••W

hat is the range of integers we can

What is the range of integers w

e can represent using signed m

agnituderepresent using signed m

agnitude––

in a byte?in a byte?

43

––in a half w

ord?in a half w

ord?

––in a w

ord?in a w

ord?

––in a double w

ord?in a double w

ord?

Signed M

agnitudeS

igned Magnitude

••H

ow do w

e know if a num

ber is positive or H

ow do w

e know if a num

ber is positive or negative?negative?

••H

ow do w

e negate a number using signed

How

do we negate a num

ber using signed

44

••H

ow do w

e negate a number using signed

How

do we negate a num

ber using signed m

agnitude?m

agnitude?

••W

hat is a disadvantage of signed W

hat is a disadvantage of signed m

agnitude?m

agnitude?

Page 23: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

One’s C

omplem

entO

ne’s Com

plement

••A

nother representation for negative A

nother representation for negative integersintegers––

Positive integers as usual

Positive integers as usual

––N

egative integers are formed by taking the

Negative integers are form

ed by taking the

45

––N

egative integers are formed by taking the

Negative integers are form

ed by taking the positive integer w

ith the same m

agnitude and positive integer w

ith the same m

agnitude and flipping every bitflipping every bit

One’s C

omplem

entO

ne’s Com

plement

••H

ow do w

e represent 100H

ow do w

e represent 1001010

as a byte?as a byte?

46

••H

ow do w

e represent H

ow do w

e represent --100100

1010as a byte?as a byte?

Page 24: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

One’s C

omplem

entO

ne’s Com

plement

••W

hat is the range of integers we can

What is the range of integers w

e can represent using one’s com

plement?

represent using one’s complem

ent?––

in a byte?in a byte?

47

––in a half w

ord?in a half w

ord?

––in a w

ord?in a w

ord?

––in a double w

ord?in a double w

ord?

One’s C

omplem

entO

ne’s Com

plement

••H

ow do w

e know if a num

ber is positive or H

ow do w

e know if a num

ber is positive or negative?negative?

••H

ow do w

e negate a number using one’s

How

do we negate a num

ber using one’s

48

••H

ow do w

e negate a number using one’s

How

do we negate a num

ber using one’s com

plement?

complem

ent?

••W

hat is a disadvantage of one’s W

hat is a disadvantage of one’s com

plement?

complem

ent?

Page 25: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

Tw

o’s Com

plement

Tw

o’s Com

plement

••A

nother representation for negative A

nother representation for negative integersintegers––

Positive integers as usual

Positive integers as usual

––N

egative integers are formed by taking the

Negative integers are form

ed by taking the

49

––N

egative integers are formed by taking the

Negative integers are form

ed by taking the positive integer w

ith the same m

agnitude and positive integer w

ith the same m

agnitude and flipping every bit, then adding oneflipping every bit, then adding one

Tw

o’s Com

plement

Tw

o’s Com

plement

••H

ow do w

e represent 100H

ow do w

e represent 1001010

as a byte?as a byte?

50

••H

ow do w

e represent H

ow do w

e represent --100100

1010as a byte?as a byte?

Page 26: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

Tw

o’s Com

plement

Tw

o’s Com

plement

••W

hat is the range of integers we can

What is the range of integers w

e can represent using tw

o’s complem

ent?represent using tw

o’s complem

ent?––

in a byte?in a byte?

51

––in a half w

ord?in a half w

ord?

––in a w

ord?in a w

ord?

––in a double w

ord?in a double w

ord?

Tw

o’s Com

plement

Tw

o’s Com

plement

••H

ow do w

e know if a num

ber is positive or H

ow do w

e know if a num

ber is positive or negative?negative?

••H

ow do w

e negate a number using tw

o’s H

ow do w

e negate a number using tw

o’s

52

••H

ow do w

e negate a number using tw

o’s H

ow do w

e negate a number using tw

o’s com

plement?

complem

ent?

••W

hat is the advantage of two’s

What is the advantage of tw

o’s com

plement?

complem

ent?

Page 27: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

Representing N

egative Num

bersR

epresenting Negative N

umbers

••In all three system

sIn all three system

s––

Positive num

bers are represented using P

ositive numbers are represented using

positional representationpositional representation

––A

number is negative if its left m

ost bit is oneA

number is negative if its left m

ost bit is one

53

––A

number is negative if its left m

ost bit is oneA

number is negative if its left m

ost bit is one

••T

he same sequence of bits represents a

The sam

e sequence of bits represents a different negative integer in each systemdifferent negative integer in each system

Addition

Addition

••H

ow do w

e add in base 2?H

ow do w

e add in base 2?

0000

+0

+0

+1

+1

54

1111

+0

+0

+1

+1

Page 28: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

Logic Gates

Logic Gates

55

Addition

Addition

••A

dding larger binary numbers

Adding larger binary num

bers

0101010101010101

+00001111

+00001111

56

+00001111

+00001111

0101010101010101

+00100101

+00100101

Page 29: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

Logic Gates

Logic Gates

57

Signed M

agnitude Addition

Signed M

agnitude Addition

••S

imilar to addition in base 10

Sim

ilar to addition in base 10––

If both operands have the same sign

If both operands have the same sign

••A

dd the magnitudes

Add the m

agnitudes••

Result has sam

e sign as operandsR

esult has same sign as operands

58

••R

esult has same sign as operands

Result has sam

e sign as operands

––If operands have different signsIf operands have different signs

••S

ubtract the smaller operand from

the larger S

ubtract the smaller operand from

the larger operandoperand

••R

esult has the same sign as the larger operand

Result has the sam

e sign as the larger operand

Page 30: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

One’s C

omplem

ent Addition

One’s C

omplem

ent Addition

••A

ddition is the same for any com

bination A

ddition is the same for any com

bination of positive and negative integersof positive and negative integers––

Add the bits together w

ith carries handled in A

dd the bits together with carries handled in

the usual way

the usual way

59

the usual way

the usual way

––If there is a carry out of the left m

ost position If there is a carry out of the left m

ost position perform

an ‘endperform

an ‘end--around’ carry and continue around’ carry and continue

addingadding

One’s C

omplem

ent Addition

One’s C

omplem

ent Addition

••E

xamples:

Exam

ples:

1001001010010010

1001001010010010

+01100111

+01100111

+11101111

+11101111

60

+01100111

+01100111

+11101111

+11101111

Page 31: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

Tw

o’s Com

plement A

dditionT

wo’s C

omplem

ent Addition

••A

ddition is the same for any com

bination A

ddition is the same for any com

bination of positive and negative num

bersof positive and negative num

bers––

Add the bits together w

ith carries handled in A

dd the bits together with carries handled in

the usual way

the usual way

61

the usual way

the usual way

––D

iscard any carry out from the left m

ost bitD

iscard any carry out from the left m

ost bit

Tw

o’s Com

plement A

dditionT

wo’s C

omplem

ent Addition

••E

xamples:

Exam

ples:

1001001010010010

1001001010010010

+11101111

+11101111

+10101111

+10101111

62

+11101111

+11101111

+10101111

+10101111

Page 32: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

Overflow

Overflow

••W

hat happens when

What happens w

hen w

e try to increment the

we try to increm

ent the counter beyond 9999?counter beyond 9999?

63

••W

hy does overflow

Why does overflow

occur in a com

puter?occur in a com

puter?

Types of O

verflowT

ypes of Overflow

••A

ny sequence of bits can be considered A

ny sequence of bits can be considered an unsigned num

beran unsigned num

ber––

Use positional representation

Use positional representation

••A

ny sequence of bits can be considered a A

ny sequence of bits can be considered a

64

••A

ny sequence of bits can be considered a A

ny sequence of bits can be considered a signed num

bersigned num

ber––

If the left most bit is 0, the num

ber is positiveIf the left m

ost bit is 0, the number is positive

••U

se positional representationU

se positional representation

––If the left m

ost bit is 1, the number is negative

If the left most bit is 1, the num

ber is negative••

Must know

what representation is being used

Must know

what representation is being used

Page 33: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

Unsigned O

verflowU

nsigned Overflow

••A

ssume all num

bers are unsignedA

ssume all num

bers are unsigned

0101010101010101

1010000010100000

+00001111

+00001111

+01111111

+01111111

65

+00001111

+00001111

+01111111

+01111111

0110010001100100

0101010101010101

1010000010100000

+01010101

+01010101

+11111111

+11111111

1010101010101010S

igned Overflow

Signed O

verflow

••A

ssume all num

bers are 2’s complem

entA

ssume all num

bers are 2’s complem

ent

0101010101010101

1010000010100000

+00001111

+00001111

+01111111

+01111111

66

+00001111

+00001111

+01111111

+01111111

0110010001100100

0101010101010101

1010000010100000

+01010101

+01010101

+11111111

+11111111

1010101010101010

Page 34: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

Overflow

Overflow

••O

verflow can be signed or unsigned

Overflow

can be signed or unsigned––

Unsigned overflow

occurs when w

e need U

nsigned overflow occurs w

hen we need

more bits to represent the answ

er than we

more bits to represent the answ

er than we

havehave

67

havehave

––S

igned overflow occurs w

hen we have tw

o S

igned overflow occurs w

hen we have tw

o num

bers in num

bers in nnbits w

ith the same sign and get

bits with the sam

e sign and get a result in n bits w

ith the opposite signa result in n bits w

ith the opposite sign••

Signed overflow

never occurs when the input

Signed overflow

never occurs when the input

numbers have opposite signs

numbers have opposite signs

Practice

Practice

68

Page 35: Digital Logicpeople.ucalgary.ca/~bdstephe/231_W08/Topic2_Numbers_2up.pdf · 2008. 1. 7. · True may be abbreviated with T or 1 ––False may be abbreviated with F or 0 False may

Finishing U

pF

inishing Up

••W

e have seen:W

e have seen:––

Basics of Logic

Basics of Logic••

Truth tables and logic gates

Truth tables and logic gates

––N

umber S

ystems

Num

ber System

s

69

––N

umber S

ystems

Num

ber System

s••

Binary and hexadecim

alB

inary and hexadecimal

––R

epresenting numbers in a com

puterR

epresenting numbers in a com

puter••

Positional representation

Positional representation

••S

igned magnitude, one’s com

plement and tw

o’s S

igned magnitude, one’s com

plement and tw

o’s com

plement

complem

ent

Finishing U

pF

inishing Up

••W

e have seen:W

e have seen:––

Addition in binary

Addition in binary••

Positive num

bersP

ositive numbers

••N

egativeN

egative

70

••N

egativeN

egative••

Overflow

Overflow