CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions...

100
CS 64 Week 1 Lecture 1 Kyle Dewey

Transcript of CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions...

Page 1: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

CS 64 Week 1 Lecture 1Kyle Dewey

Page 2: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Overview

• Bitwise operation wrap-up

• Two’s complement

• Addition

• Subtraction

• Multiplication (if time)

Page 3: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Bitwise Operation Wrap-up

Page 4: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Shift Left

• Move all the bits N positions to the left, subbing in N 0s on the right

Page 5: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Shift Left

• Move all the bits N positions to the left, subbing in N 0s on the right

1001

Page 6: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Shift Left

• Move all the bits N positions to the left, subbing in N 0s on the right

1001 << 2 =100100

Page 7: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Shift Left

• Useful as a restricted form of multiplication

• Question: how?

1001 << 2 =100100

Page 8: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Shift Left as Multiplication

• Equivalent decimal operation:

234

Page 9: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Shift Left as Multiplication

• Equivalent decimal operation:

234 << 1 =2340

Page 10: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Shift Left as Multiplication

• Equivalent decimal operation:

234 << 1 =2340

234 << 2 =23400

Page 11: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Multiplication• Shifting left N positions multiplies by (base)N

• Multiplying by 2 or 4 is often necessary (shift left 1 or 2 positions, respectively)

• Often a whooole lot faster than telling the processor to multiply

• Compilers try hard to do this

234 << 2 =23400

Page 12: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Shift Right

• Move all the bits N positions to the right, subbing in either N 0s or N 1s on the left

• Two different forms

Page 13: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Shift Right

• Move all the bits N positions to the right, subbing in either N 0s or N (whatever the leftmost bit is)s on the left

• Two different forms1001 >> 2 =either 0010 or 1110

Page 14: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Shift Right Trick

• Question: If shifting left multiplies, what does shift right do?

Page 15: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Shift Right Trick

• Question: If shifting left multiplies, what does shift right do?

• Answer: divides in a similar way, but truncates result

Page 16: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Shift Right Trick

• Question: If shifting left multiplies, what does shift right do?

• Answer: divides in a similar way, but truncates result

234

Page 17: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Shift Right Trick

• Question: If shifting left multiplies, what does shift right do?

• Answer: divides in a similar way, but truncates result

234 >> 1 =23

Page 18: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Two Forms of Shift Right

• Subbing in 0s makes sense

• What about subbing in the leftmost bit?

• And why is this called “arithmetic” shift right?

1100 (arithmetic)>> 1 =1110

Page 19: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Answer...Sort of

• Arithmetic form is intended for numbers in twos complement, whereas the non-arithmetic form is intended for unsigned numbers

Page 20: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Twos Complement

Page 21: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Problem

• Binary representation so far makes it easy to represent positive numbers and zero

• Question: What about representing negative numbers?

Page 22: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Twos Complement

• Way to represent positive integers, negative integers, and zero

• If 1 is in the most significant bit (generally leftmost bit in this class), then it is negative

Page 23: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Decimal to Twos Complement

• Example: -5 decimal to binary (twos complement)

Page 24: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Decimal to Twos Complement

• Example: -5 decimal to binary (twos complement)

• First, convert the magnitude to an unsigned representation

Page 25: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Decimal to Twos Complement

• Example: -5 decimal to binary (twos complement)

• First, convert the magnitude to an unsigned representation

5 (decimal) = 0101 (binary)

Page 26: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Decimal to Twos Complement

• Then, take the bits, and negate them

Page 27: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Decimal to Twos Complement

• Then, take the bits, and negate them

0101

Page 28: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Decimal to Twos Complement

• Then, take the bits, and negate them

~0101 = 1010

Page 29: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Decimal to Twos Complement

• Finally, add one:

Page 30: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Decimal to Twos Complement

• Finally, add one:

1010

Page 31: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Decimal to Twos Complement

• Finally, add one:1010 + 1 =1011

Page 32: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Twos Complement to Decimal

• Same operation: negate the bits, and add one

Page 33: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Twos Complement to Decimal

• Same operation: negate the bits, and add one

1011

Page 34: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Twos Complement to Decimal

• Same operation: negate the bits, and add one

~1011 = 0100

Page 35: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Twos Complement to Decimal

• Same operation: negate the bits, and add one

0100

Page 36: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Twos Complement to Decimal

• Same operation: negate the bits, and add one

0100 + 1 =0101

Page 37: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Twos Complement to Decimal

• Same operation: negate the bits, and add one

0100 + 1 =0101 =-5

We started with 1011 - negative

Page 38: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Where Is Twos Complement From?

• Intuition: try to subtract 1 from 0, in decimal

• Involves borrowing from an invisible number on the left

• Twos complement is based on the same idea

Page 39: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Another View• Modular arithmetic, with the convention that a

leading 1 bit means negative

000

001

010

011

100

101

110

111Denotes+1

Page 40: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Another View• Modular arithmetic, with the convention that a

leading 1 bit means negative

000

001

010

011

100

101

110

111Denotes+1

(least +)(least -)

(most +)

(most -)

(zero)

Page 41: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Another View• Modular arithmetic, with the convention that a

leading 1 bit means negative

000

001

010

011

100

101

110

111Denotes+1

1

0

2

3

-4

-3

-2

-1

Page 42: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Negation of 1

000

001

010

011

100

101

110

111

Page 43: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Negation of 1

000

001

010

011

100

101

110

111

Page 44: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Negation of 1

000

001

010

011

100

101

110

111

Page 45: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Negation of 1

000

001

010

011

100

101

110

111

Page 46: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Consequences

• What is the negation of 000?

000

001

010

011

100

101

110

111

Page 47: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Consequences

• What is the negation of 100?

000

001

010

011

100

101

110

111

Page 48: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Arithmetic Shift Right• Not exactly division by a power of two

• Consider -3 / 2

000

001

010

011

100

101

110

111 1

0

2

3

-4

-3

-2

-1

Page 49: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Addition

Page 50: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Building Up Addition• Question: how might we add the following,

in decimal?986

+123----

?

Page 51: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Building Up Addition• Question: how might we add the following,

in decimal?986

+123----

?

6+3--?

Page 52: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Building Up Addition• Question: how might we add the following,

in decimal?986

+123----

?

6+3--9

8+2--?

Page 53: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Building Up Addition• Question: how might we add the following,

in decimal?986

+123----

?

6+3--9

8+2--0

Carry: 1

Page 54: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Building Up Addition• Question: how might we add the following,

in decimal?986

+123----

?

6+3--9

8+2--0

19

+1--?

Page 55: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Building Up Addition• Question: how might we add the following,

in decimal?986

+123----

?

6+3--9

8+2--0

19

+1--1

Carry: 1

Page 56: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Building Up Addition• Question: how might we add the following,

in decimal?986

+123----

?

6+3--9

8+2--0

19

+1--1

1+0--1

Page 57: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Core Concepts

• We have a “primitive” notion of adding single digits, along with an idea of carrying digits

• We can build on this notion to add numbers together that are more than one digit long

Page 58: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Now in Binary

• Arguably simpler - fewer one-bit possibilities

0+0--?

0+1--?

1+0--?

1+1--?

Page 59: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Now in Binary

• Arguably simpler - fewer one-bit possibilities

0+0--0

0+1--1

1+0--1

1+1--0

Carry: 1

Page 60: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Chaining the Carry• Also need to account for any input carry

10

+0--1

10

+1--0

11

+0--0

11

+1--1

00

+0--0

00

+1--1

01

+0--1

01

+1--0

Carry: 1 Carry: 1 Carry: 1

Carry: 1

Page 61: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

In Summary

Single Bit Adder

First OperandBit

Second OperandBit

Input CarryBit

Result Bit Output CarryBit

Page 62: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

• How can we adapt this to add multi-digit binary numbers together?

Single Bit Adder

First OperandBit

Second OperandBit

Input CarryBit

Result Bit Output CarryBit

Page 63: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Putting it Together

Single Bit Adder

First OperandBit

Second OperandBit

Input CarryBit

Result Bit Output CarryBit

Page 64: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Putting it Together

+

I1 I2

CI CO

R

Page 65: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Putting it Together

+

A0 B0

0

R0

+

R1

+ C

R2

For two three-bit numbers, A and B, resulting ina three-bit result R

A1 B1 A2 B2

Page 66: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Output Carry

+

A0 B0

0

R0

+

R1

+ C

R2

What about the output carry bit?

A1 B1 A2 B2

Page 67: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Output Carry

+

A0 B0

0

R0

+

R1

+ C

R2

What about the output carry bit?

A1 B1 A2 B2

A: 111B: 001

R: ?, C: ?

Page 68: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Output Carry

+

A0 B0

0

R0

+

R1

+ C

R2

What about the output carry bit?

A1 B1 A2 B2

A: 111B: 001

R: 000, C: 1

Page 69: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Output Carry Bit Significance

• For unsigned numbers, it indicates if the result did not fit all the way into the number of bits allotted

• May be an error condition for software

Page 70: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Signed Addition

• Question: what is the result of the following operation?

011+011----

?

Page 71: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Signed Addition

• Question: what is the result of the following operation?

011+011----0111

Page 72: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Overflow

• In this situation, overflow occurred: this means that both the operands had the same sign, and the result’s sign differed

011+011----110

• Possibly a software error

Page 73: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Overflow vs. Carry• These are different ideas

• Carry is relevant to unsigned values

• Overflow is relevant to signed values

011+011----111

Overflow; No Carry

111+001----000

No Overflow; Carry

111+100----011

Overflow; Carry

001+001----010

No Overflow; No Carry

Page 74: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Subtraction

Page 75: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Subtraction

• Have been saying to invert bits and add one to second operand

• Could do it this way in hardware, but there is a trick

001-001----

?

001+111----

?

(equivalent to)

Page 76: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Subtraction Trick• Assume we can invert bits, but we cannot

add one in a separate step

• How might we make this work given only our three-bit adder from before?

+

A0 B0

0

R0

+

R1

+ C

R2

A1 B1 A2 B2

Page 77: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Subtraction Trick

• Put in an initial carry of 1: this indicates to add 1 anyways

+

A0 B0

1

R0

+

R1

+ C

R2

A1 B1 A2 B2

Page 78: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Multiplication (if time)

Page 79: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Multiplication

• For simplicity, we will only consider positive values here

• A number of different algorithms exist; we will only look at one of them

Page 80: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Central Idea

• Accumulate a partial product: the result of the multiplication as we go on

• Computed via a series of additions

• When we are finished, the partial product becomes the final product (the result)

• Build off of addition and multiplication of a single digit (much like with addition)

Page 81: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Decimal Algorithm• Let P be the partial product, M be the

multiplicand, and N be the multiplier

• Initially, P is 0

• If N is 0, then P = the result

• If not, then P += (the rightmost digit of N) times M

• Shift N right once, and M left once

• Repeat

Page 82: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Example• Performing 803 * 151

Page 83: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Example• Performing 803 * 151

P M N

Page 84: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Example• Performing 803 * 151

P M N

0 803 151Initially P = 0,N = multiplicandM = multiplier

Page 85: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Example• Performing 803 * 151

P M N

0 803 151 N is not 0

Page 86: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Example• Performing 803 * 151

P M N

0 803 151

803

P += (the rightmost digit of N) times M

Page 87: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Example• Performing 803 * 151

P M N

0 803 151

803 8030 15

Shift N right once, and M left once

Page 88: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Example• Performing 803 * 151

P M N

0 803 151

803 8030 15 N is not 0

Page 89: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Example• Performing 803 * 151

P M N

0 803 151

803 8030 15

40953

P += (the rightmost digit of N) times M

Page 90: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Example• Performing 803 * 151

P M N

0 803 151

803 8030 15

40953 80300 1

Shift N right once, and M left once

Page 91: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Example• Performing 803 * 151

P M N

0 803 151

803 8030 15

40953 80300 1 N is not 0

Page 92: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Example• Performing 803 * 151

P M N

0 803 151

803 8030 15

40953 80300 1

121253

P += (the rightmost digit of N) times M

Page 93: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Example• Performing 803 * 151

P M N

0 803 151

803 8030 15

40953 80300 1

121253 803000 0

Shift N right once, and M left once

Page 94: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Example• Performing 803 * 151

P M N

0 803 151

803 8030 15

40953 80300 1

121253 803000 0 N is 0; done

Page 95: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Intuition

• Only looking at rightmost digit of N: getting partial product of that digit with the rest

• Shifting M left: for each digit of N observed, we look one digit deeper in M (and result gets correspondingly larger)

• Similar to traditional pencil-and-paper algorithm (which shifts partial products instead)

Page 96: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Why this Algorithm?• Looks complex...ish

• On binary, things get simpler. Why?

• Initially, P is 0

• If N is 0, then P = the result

• If not, then P += (the rightmost digit of N) times M

• Shift N right once, and M left once

• Repeat

Page 97: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Why this Algorithm?• Looks complex...ish

• On binary, things get simpler. Why?

• Initially, P is 0

• If N is 0, then P = the result

• If not, then P += (the rightmost digit of N) times M

• Shift N right once, and M left once

• Repeat

Page 98: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Simplified Binary Algorithm

• Initially, P is 0

• If N is 0, then P = the result

• If not, then P += (the rightmost digit of N) times M

• Shift N right once, and M left once

• Repeat

Page 99: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Simplified Binary Algorithm

• Initially, P is 0

• If N is 0, then P = the result

• If the rightmost digit if N is 1:

•P += M

• Shift N right once, and M left once

• Repeat

Page 100: CS 64 Week 1 Lecture 1 - kyledewey.github.io · Multiplication • Shifting left N positions multiplies by (base)N • Multiplying by 2 or 4 is often necessary (shift left 1 or 2

Dealing with Negative Numbers

• Can still be done, but we need extra logic

• Negative times negative is a positive, positive and a negative is a positive...

• Not fundamentally harder, and showing this extra detail just complicates things in an uninteresting way