CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1...
Transcript of CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1...
![Page 1: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/1.jpg)
CS 64 Week 1 Lecture 1Kyle Dewey
![Page 2: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/2.jpg)
Overview
• Bitwise operation wrap-up
• Two’s complement
• Addition
• Subtraction
• Multiplication (if time)
![Page 3: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/3.jpg)
Bitwise Operation Wrap-up
![Page 4: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/4.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/5.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/6.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/7.jpg)
Shift Left
• Useful as a restricted form of multiplication
• Question: how?
1001 << 2 =100100
![Page 8: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/8.jpg)
Shift Left as Multiplication
• Equivalent decimal operation:
234
![Page 9: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/9.jpg)
Shift Left as Multiplication
• Equivalent decimal operation:
234 << 1 =2340
![Page 10: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/10.jpg)
Shift Left as Multiplication
• Equivalent decimal operation:
234 << 1 =2340
234 << 2 =23400
![Page 11: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/11.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/12.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/13.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/14.jpg)
Shift Right Trick
• Question: If shifting left multiplies, what does shift right do?
![Page 15: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/15.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/16.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/17.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/18.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/19.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/20.jpg)
Twos Complement
![Page 21: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/21.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/22.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/23.jpg)
Decimal to Twos Complement
• Example: -5 decimal to binary (twos complement)
![Page 24: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/24.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/25.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/26.jpg)
Decimal to Twos Complement
• Then, take the bits, and negate them
![Page 27: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/27.jpg)
Decimal to Twos Complement
• Then, take the bits, and negate them
0101
![Page 28: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/28.jpg)
Decimal to Twos Complement
• Then, take the bits, and negate them
~0101 = 1010
![Page 29: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/29.jpg)
Decimal to Twos Complement
• Finally, add one:
![Page 30: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/30.jpg)
Decimal to Twos Complement
• Finally, add one:
1010
![Page 31: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/31.jpg)
Decimal to Twos Complement
• Finally, add one:1010 + 1 =1011
![Page 32: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/32.jpg)
Twos Complement to Decimal
• Same operation: negate the bits, and add one
![Page 33: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/33.jpg)
Twos Complement to Decimal
• Same operation: negate the bits, and add one
1011
![Page 34: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/34.jpg)
Twos Complement to Decimal
• Same operation: negate the bits, and add one
~1011 = 0100
![Page 35: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/35.jpg)
Twos Complement to Decimal
• Same operation: negate the bits, and add one
0100
![Page 36: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/36.jpg)
Twos Complement to Decimal
• Same operation: negate the bits, and add one
0100 + 1 =0101
![Page 37: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/37.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/38.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/39.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/40.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/41.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/42.jpg)
Negation of 1
000
001
010
011
100
101
110
111
![Page 43: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/43.jpg)
Negation of 1
000
001
010
011
100
101
110
111
![Page 44: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/44.jpg)
Negation of 1
000
001
010
011
100
101
110
111
![Page 45: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/45.jpg)
Negation of 1
000
001
010
011
100
101
110
111
![Page 46: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/46.jpg)
Consequences
• What is the negation of 000?
000
001
010
011
100
101
110
111
![Page 47: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/47.jpg)
Consequences
• What is the negation of 100?
000
001
010
011
100
101
110
111
![Page 48: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/48.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/49.jpg)
Addition
![Page 50: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/50.jpg)
Building Up Addition• Question: how might we add the following,
in decimal?986
+123----
?
![Page 51: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/51.jpg)
Building Up Addition• Question: how might we add the following,
in decimal?986
+123----
?
6+3--?
![Page 52: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/52.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/53.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/54.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/55.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/56.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/57.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/58.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/59.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/60.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/61.jpg)
In Summary
Single Bit Adder
First OperandBit
Second OperandBit
Input CarryBit
Result Bit Output CarryBit
![Page 62: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/62.jpg)
• 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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/63.jpg)
Putting it Together
Single Bit Adder
First OperandBit
Second OperandBit
Input CarryBit
Result Bit Output CarryBit
![Page 64: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/64.jpg)
Putting it Together
+
I1 I2
CI CO
R
![Page 65: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/65.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/66.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/67.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/68.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/69.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/70.jpg)
Signed Addition
• Question: what is the result of the following operation?
011+011----
?
![Page 71: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/71.jpg)
Signed Addition
• Question: what is the result of the following operation?
011+011----0111
![Page 72: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/72.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/73.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/74.jpg)
Subtraction
![Page 75: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/75.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/76.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/77.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/78.jpg)
Multiplication (if time)
![Page 79: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/79.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/80.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/81.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/82.jpg)
Example• Performing 803 * 151
![Page 83: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/83.jpg)
Example• Performing 803 * 151
P M N
![Page 84: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/84.jpg)
Example• Performing 803 * 151
P M N
0 803 151Initially P = 0,N = multiplicandM = multiplier
![Page 85: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/85.jpg)
Example• Performing 803 * 151
P M N
0 803 151 N is not 0
![Page 86: CS 64 Week 1 Lecture 1 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/86.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/87.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/88.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/89.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/90.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/91.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/92.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/93.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/94.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/95.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/96.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/97.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/98.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/99.jpg)
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 - UCSBkyledewey/cs64f15/lectures/week1/lecture_1.pdfCS 64 Week 1 Lecture 1 Kyle Dewey. Overview ... 234](https://reader034.fdocuments.net/reader034/viewer/2022051815/603f74c4fb09c232ed02d851/html5/thumbnails/100.jpg)
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