lecture 2 binary arithmetic topics terminology fractions base-r to decimal unsigned integers signed...

25
Lecture 2 Binary Arithmetic Topics Topics Terminology Fractions Base-r to decimal Unsigned Integers Signed magnitude Two’s complement August 26, 2003 CSCE 211 Digital Design

Upload: amie-watson

Post on 18-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 2 Binary Arithmetic Topics Terminology Fractions Base-r to decimal Unsigned Integers Signed magnitude Two’s complement August 26, 2003 CSCE 211

Lecture 2Binary Arithmetic

Lecture 2Binary Arithmetic

TopicsTopics

Terminology Fractions Base-r to decimal Unsigned Integers Signed magnitude Two’s complement

August 26, 2003

CSCE 211 Digital Design

Page 2: Lecture 2 Binary Arithmetic Topics Terminology Fractions Base-r to decimal Unsigned Integers Signed magnitude Two’s complement August 26, 2003 CSCE 211

– 2 – CSCE 211H Fall 2003

OverviewOverview

Last Time: Readings 1.1, 1.2 2.1-2.3 Last Time: Readings 1.1, 1.2 2.1-2.3 Analog vs Digital Conversion Base-r to decimal Conversion decimal to Base-r

New: Readings 2.4-2.10, 3.1New: Readings 2.4-2.10, 3.1 Conversion of Fractions base-r decimal Unsigned Arithmetic Signed Magnitude Two’s Complement Excess-1023

VHDLVHDL Introductory Examples

Page 3: Lecture 2 Binary Arithmetic Topics Terminology Fractions Base-r to decimal Unsigned Integers Signed magnitude Two’s complement August 26, 2003 CSCE 211

– 3 – CSCE 211H Fall 2003

Review Last Time: Base-r to decimalReview Last Time: Base-r to decimal

Converting base-r to decimal by definitionConverting base-r to decimal by definition

ddnndd

n-1n-1dd

n-2n-2…d…d

2 2dd

1 1dd

0 0 = d = d

nnrrnn + d + d

n-1n-1rrn-1n-1… d… d

22rr22 +d +d

1 1rr1 + 1 + dd

0 0rr00

ExampleExample

4F0C 4F0C = = 4*164*1633 + F*16 + F*1622 + 0*16 + 0*1611 + C*16 + C*1600

== 4*4096 + 15*256 + 0 + 12*1 4*4096 + 15*256 + 0 + 12*1

= = 16384 + 3840 + 1216384 + 3840 + 12

== 2023620236

Page 4: Lecture 2 Binary Arithmetic Topics Terminology Fractions Base-r to decimal Unsigned Integers Signed magnitude Two’s complement August 26, 2003 CSCE 211

– 4 – CSCE 211H Fall 2003

Review Last Time: decimal to Base-rReview Last Time: decimal to Base-r

Repeated division algorithmRepeated division algorithm

Justification:Justification:

ddnndd

n-1n-1dd

n-2n-2…d…d

2 2dd

1 1dd

0 0 = d = d

nnrrnn + d + d

n-1n-1rrn-1n-1… d… d

22rr22 +d +d

1 1rr1 + 1 + dd

0 0rr00

Dividing each side by r yieldsDividing each side by r yields

(d(dnndd

n-1n-1dd

n-2n-2…d…d

2 2dd

1 1dd

0 0) / r = d) / r = d

nnrrn-1n-1 + d + d

n-1n-1rrn-2n-2… d… d

22rr11+d+d

1 1rr0 + 0 + dd

0 0rr-1-1

So dSo d 0 0 is the remainder of the first division is the remainder of the first division

((q((q11) / r = d) / r = d

nnrrn-2n-2 + d + d

n-1n-1rrn-3n-3… d… d

33rr11+d+d

2 2rr0 + 0 + dd

1 1rr-1-1

So dSo d 1 1 is the remainder of the next division is the remainder of the next division

and dand d 2 2 is the remainder of the next division is the remainder of the next division

……

Page 5: Lecture 2 Binary Arithmetic Topics Terminology Fractions Base-r to decimal Unsigned Integers Signed magnitude Two’s complement August 26, 2003 CSCE 211

– 5 – CSCE 211H Fall 2003

Review Last Time: decimal to Base-rReview Last Time: decimal to Base-r

Repeated division algorithm ExampleRepeated division algorithm Example

Convert 4343 to hexConvert 4343 to hex

4343/16 = 271 remainder = 74343/16 = 271 remainder = 7

271/16 = 16 remainder = 15271/16 = 16 remainder = 15

16/16 = 1 remainder = 0 16/16 = 1 remainder = 0

1/16 = 0 remainder = 11/16 = 0 remainder = 1

So 4343So 43431010

= 10F7 = 10F71616

To check the answer convert back to decimalTo check the answer convert back to decimal

10F7 = 1*1610F7 = 1*1633 + 15*16 + 7*1 = 4096 + 240 + 7 = 4343 + 15*16 + 7*1 = 4096 + 240 + 7 = 4343

Page 6: Lecture 2 Binary Arithmetic Topics Terminology Fractions Base-r to decimal Unsigned Integers Signed magnitude Two’s complement August 26, 2003 CSCE 211

– 6 – CSCE 211H Fall 2003

Review Last Time: Hex to BinaryReview Last Time: Hex to Binary

One hex digit = four binary digitsOne hex digit = four binary digits

ExampleExample

3FAC = 0011 1111 1010 1100 3FAC = 0011 1111 1010 1100 (spaces just for readability)(spaces just for readability)

Binary to hex four digits = one (from right!!!)Binary to hex four digits = one (from right!!!)

ExampleExample

111101001111010 = 0011 1101 0011 1010111101001111010 = 0011 1101 0011 1010

= 3 D A= 3 D A

Page 7: Lecture 2 Binary Arithmetic Topics Terminology Fractions Base-r to decimal Unsigned Integers Signed magnitude Two’s complement August 26, 2003 CSCE 211

– 7 – CSCE 211H Fall 2003

Hex to Decimal FractionsHex to Decimal Fractions

.d.d-1-1dd

-2-2dd

-3-3…d…d

–(n-2) –(n-2)dd

–(n-1) –(n-1)dd

-n -n= d= d

-1-1rr-1-1 + d + d

-2-2rr-2-2…d…d

-(n-1)-(n-1)rr-(n-1)-(n-1) +d +d

1 1rr-n-n

ExampleExample

.1EF.1EF1616

= 1*16-1 + E*16-2 + F*16-3 = 1*16-1 + E*16-2 + F*16-3

= 1*.0625 + 14*.003906025 + 15*2.4414e-= 1*.0625 + 14*.003906025 + 15*2.4414e-44

= .117201… (probably close but not = .117201… (probably close but not right)right)

Page 8: Lecture 2 Binary Arithmetic Topics Terminology Fractions Base-r to decimal Unsigned Integers Signed magnitude Two’s complement August 26, 2003 CSCE 211

– 8 – CSCE 211H Fall 2003

Decimal Fractions to hexDecimal Fractions to hex

.d.d-1-1dd

-2-2dd

-3-3…d…d

–(n-2) –(n-2)dd

–(n-1) –(n-1)dd

-n -n= d= d

-1-1rr-1-1 + d + d

-2-2rr-2-2…d…d

-(n-1)-(n-1)rr-(n-1)-(n-1) +d +d

1 1rr-n-n

Multiplication by r yields Multiplication by r yields

r *(.dr *(.d-1-1rr-1-1 + d + d

-2-2rr-2-2…d…d

-(n-1)-(n-1)rr-(n-1)-(n-1) +d +d

1 1rr-n-n

= d= d-1-1rr00 + d + d

-2-2rr-1-1…d…d

-(n-1)-(n-1)rr-(n-2)-(n-2) +d +d

1 1rr-(n-1)-(n-1)

Whole number part = dWhole number part = d-1-1rr00

Multiplying again by r yields dMultiplying again by r yields d-2-2rr0 0 as the whole number as the whole number

partpart

… … till fraction = 0till fraction = 0

Page 9: Lecture 2 Binary Arithmetic Topics Terminology Fractions Base-r to decimal Unsigned Integers Signed magnitude Two’s complement August 26, 2003 CSCE 211

– 9 – CSCE 211H Fall 2003

Example: Hex Fractions to decimalExample: Hex Fractions to decimal

Convert .3FA to decimalConvert .3FA to decimal

.3FA16 = 3*16-1 + F*16-2 + A*16-3.3FA16 = 3*16-1 + F*16-2 + A*16-3

= 3*.0625 + 15*.00390625 +10* (1/4096)= 3*.0625 + 15*.00390625 +10* (1/4096)

= .191162109= .191162109

Page 10: Lecture 2 Binary Arithmetic Topics Terminology Fractions Base-r to decimal Unsigned Integers Signed magnitude Two’s complement August 26, 2003 CSCE 211

– 10 – CSCE 211H Fall 2003

Unsigned integersUnsigned integers

What is the biggest integer representable using n-bits(n What is the biggest integer representable using n-bits(n digits)?digits)?

What is its value in decimal?What is its value in decimal?

Special casesSpecial cases

8 bits8 bits

16 bits16 bits

32 bits32 bits

Page 11: Lecture 2 Binary Arithmetic Topics Terminology Fractions Base-r to decimal Unsigned Integers Signed magnitude Two’s complement August 26, 2003 CSCE 211

– 11 – CSCE 211H Fall 2003

Arithmetic with Binary NumbersArithmetic with Binary Numbers

10010110 10010110 1001011010010110 11011101

+00110111+00110111 - 00110111- 00110111 x 101x 101

Problems with 8 bit operationsProblems with 8 bit operations

1001011010010110

++ 1001011010010110

Page 12: Lecture 2 Binary Arithmetic Topics Terminology Fractions Base-r to decimal Unsigned Integers Signed magnitude Two’s complement August 26, 2003 CSCE 211

– 12 – CSCE 211H Fall 2003

Signed integersSigned integers

How do we represent?How do we represent?

Signed-magnitudeSigned-magnitude

Excess representationsExcess representations

w bits w bits 0 <= unsigned_value < 2 0 <= unsigned_value < 2ww

In excess-B we subtract the bias (B) to get the value.In excess-B we subtract the bias (B) to get the value.

exampleexample

Page 13: Lecture 2 Binary Arithmetic Topics Terminology Fractions Base-r to decimal Unsigned Integers Signed magnitude Two’s complement August 26, 2003 CSCE 211

– 13 – CSCE 211H Fall 2003

Complement Representations of Signed integersComplement Representations of Signed integers

One’s complementOne’s complement

Two’s complementTwo’s complement

Page 14: Lecture 2 Binary Arithmetic Topics Terminology Fractions Base-r to decimal Unsigned Integers Signed magnitude Two’s complement August 26, 2003 CSCE 211

– 14 – CSCE 211H Fall 2003

Two’s Complement OperationTwo’s Complement Operation

One’s complement + 1 orOne’s complement + 1 or

Find rightmost 1, complement all bits to the left of it.Find rightmost 1, complement all bits to the left of it.

ExamplesExamples

0100111001001110 0000000100000001 0000000000000000 0000001000000010

Page 15: Lecture 2 Binary Arithmetic Topics Terminology Fractions Base-r to decimal Unsigned Integers Signed magnitude Two’s complement August 26, 2003 CSCE 211

– 15 – CSCE 211H Fall 2003

Two’s Complement RepresentationTwo’s Complement Representation

Consider a two’s complement binary numberConsider a two’s complement binary number

ddnndd

n-1n-1dd

n-2n-2…d…d

2 2dd

1 1dd

0 0

If dIf dn n , the sign bit = 0 the number is positive and its , the sign bit = 0 the number is positive and its

magnitude is given by the other bits.magnitude is given by the other bits.

If dIf dn n , the sign bit = 1 the number is negative and take its , the sign bit = 1 the number is negative and take its

two’s complement to get the magnitude.two’s complement to get the magnitude.

Weighted Sum InterpretationWeighted Sum Interpretation

0 10 1

1 21 2

……

n-1 2 n-1 2 n-2n-2

n -2 n -2 n-1n-1

Page 16: Lecture 2 Binary Arithmetic Topics Terminology Fractions Base-r to decimal Unsigned Integers Signed magnitude Two’s complement August 26, 2003 CSCE 211

– 16 – CSCE 211H Fall 2003

Two’s Complement RepresentationTwo’s Complement Representation

Consider a two’s complement binary numberConsider a two’s complement binary number

ddnndd

n-1n-1dd

n-2n-2…d…d

2 2dd

1 1dd

0 0

If dIf dn n , the sign bit = 0 the number is positive and its , the sign bit = 0 the number is positive and its

magnitude is given by the other bits.magnitude is given by the other bits.

If dIf dn n , the sign bit = 1 the number is negative and take its , the sign bit = 1 the number is negative and take its

two’s complement to get the magnitude.two’s complement to get the magnitude.

Weighted SumWeighted Sum

0 10 1 Example 10010011 = Example 10010011 = 1 21 2

……

n-1 2 n-1 2 n-2n-2

n -2 n -2 n-1n-1

Page 17: Lecture 2 Binary Arithmetic Topics Terminology Fractions Base-r to decimal Unsigned Integers Signed magnitude Two’s complement August 26, 2003 CSCE 211

– 17 – CSCE 211H Fall 2003

Two’s Complement RepresentationTwo’s Complement Representation

What is the 2’s complement representation in 16 bits of What is the 2’s complement representation in 16 bits of –5?–5?

+7?+7?

-1?-1?

00

-2-2

Page 18: Lecture 2 Binary Arithmetic Topics Terminology Fractions Base-r to decimal Unsigned Integers Signed magnitude Two’s complement August 26, 2003 CSCE 211

– 18 – CSCE 211H Fall 2003

Arithmetic with Signed IntegersArithmetic with Signed Integers

Signed Magnitude AdditionSigned Magnitude Addition

if the signs are the same add the magnitude if the signs are the same add the magnitude

if the signs are different subtract the smaller from if the signs are different subtract the smaller from the larger and use the sign of the largerthe larger and use the sign of the larger

Subtraction?Subtraction?

Two’s complementTwo’s complement

Just add signs take care of themselvesJust add signs take care of themselves

Page 19: Lecture 2 Binary Arithmetic Topics Terminology Fractions Base-r to decimal Unsigned Integers Signed magnitude Two’s complement August 26, 2003 CSCE 211

– 19 – CSCE 211H Fall 2003

Overflow in Two’s ComplementOverflow in Two’s Complement

Page 20: Lecture 2 Binary Arithmetic Topics Terminology Fractions Base-r to decimal Unsigned Integers Signed magnitude Two’s complement August 26, 2003 CSCE 211

– 20 – CSCE 211H Fall 2003

Binary Code DecimalBinary Code Decimal

Page 21: Lecture 2 Binary Arithmetic Topics Terminology Fractions Base-r to decimal Unsigned Integers Signed magnitude Two’s complement August 26, 2003 CSCE 211

– 21 – CSCE 211H Fall 2003

Representations of CharactersRepresentations of Characters

Page 22: Lecture 2 Binary Arithmetic Topics Terminology Fractions Base-r to decimal Unsigned Integers Signed magnitude Two’s complement August 26, 2003 CSCE 211

– 22 – CSCE 211H Fall 2003

Basic GatesBasic Gates

ANDAND

OROR

NOTNOT

Page 23: Lecture 2 Binary Arithmetic Topics Terminology Fractions Base-r to decimal Unsigned Integers Signed magnitude Two’s complement August 26, 2003 CSCE 211

– 23 – CSCE 211H Fall 2003

Basic GatesBasic Gates

NANDNAND

NORNOR

XORXOR

Page 24: Lecture 2 Binary Arithmetic Topics Terminology Fractions Base-r to decimal Unsigned Integers Signed magnitude Two’s complement August 26, 2003 CSCE 211

– 24 – CSCE 211H Fall 2003

Half Adder CircuitHalf Adder Circuit

Page 25: Lecture 2 Binary Arithmetic Topics Terminology Fractions Base-r to decimal Unsigned Integers Signed magnitude Two’s complement August 26, 2003 CSCE 211

– 25 – CSCE 211H Fall 2003

Full AdderFull Adder