CS 230 – Introduction to Computers and Computer Systems Lecture 3 – Binary Arithmetic and Two’s Complement CS 230 - Spring 2020 1-1
Binary Addition Same as regular addition Add digits right to left least significant bit (LSB) to most significant bit (MSB) carry CS 230 - Spring 2020 1-2
CS 230 - Spring 2020 1-3
CS 230 - Spring 2020 1-4
CS 230 - Spring 2020 1-5
CS 230 - Spring 2020 1-6
CS 230 - Spring 2020 1-7
CS 230 - Spring 2020 1-8
CS 230 - Spring 2020 1-9
CS 230 - Spring 2020 1-10
CS 230 - Spring 2020 1-11
CS 230 - Spring 2020 1-12
CS 230 - Spring 2020 1-13
CS 230 - Spring 2020 1-14
CS 230 - Spring 2020 1-15
CS 230 - Spring 2020 1-16
CS 230 - Spring 2020 1-17
CS 230 - Spring 2020 1-18
CS 230 - Spring 2020 1-19
CS 230 - Spring 2020 1-20
CS 230 - Spring 2020 1-21
CS 230 - Spring 2020 1-22
Binary Multiplication Same as regular multiplication Multiply bits of lower term by upper term LSB to MSB Add zeros for each bit multiplied so far Will always be zeros or copy of upper term Add copies together to get product CS 230 - Spring 2020 1-23
CS 230 - Spring 2020 1-24
CS 230 - Spring 2020 1-25
CS 230 - Spring 2020 1-26
CS 230 - Spring 2020 1-27
CS 230 - Spring 2020 1-28
CS 230 - Spring 2020 1-29
CS 230 - Spring 2020 1-30
CS 230 - Spring 2020 1-31
CS 230 - Spring 2020 1-32
CS 230 - Spring 2020 1-33
CS 230 - Spring 2020 1-34
CS 230 - Spring 2020 1-35
CS 230 - Spring 2020 1-36
CS 230 - Spring 2020 1-37
CS 230 - Spring 2020 1-38
CS 230 - Spring 2020 1-39
CS 230 - Spring 2020 1-40
CS 230 - Spring 2020 1-41
CS 230 - Spring 2020 1-42
CS 230 - Spring 2020 1-43
CS 230 - Spring 2020 1-44
CS 230 - Spring 2020 1-45
CS 230 - Spring 2020 1-46
CS 230 - Spring 2020 1-47
CS 230 - Spring 2020 1-48
CS 230 - Spring 2020 1-49
CS 230 - Spring 2020 1-50
CS 230 - Spring 2020 1-51
CS 230 - Spring 2020 1-52
CS 230 - Spring 2020 1-53
CS 230 - Spring 2020 1-54
CS 230 - Spring 2020 1-55
CS 230 - Spring 2020 1-56
CS 230 - Spring 2020 1-57
CS 230 - Spring 2020 1-58
CS 230 - Spring 2020 1-59
CS 230 - Spring 2020 1-60
CS 230 - Spring 2020 1-61
CS 230 - Spring 2020 1-62
Revisit Binary Addition We’ve been doing unsigned binary But, if you are building circuits then you have a limit to the number of bits available to represent values Fixed width representation has overflow An n -bit binary number has range Min: 0 Max: 2 n -1 CS 230 - Spring 2020 1-63
What about negative numbers? On paper we can draw a negative sign In a computer we need a bit What does this bit mean? CS 230 - Spring 2020 1-64
Signed Magnitude (BAD) Just have MSB be sign bit If it’s one the number is negative If it’s zero the number is positive Problems Two zeros Addition doesn’t work Ex: 4-bit version (1 sign bit, 3 magnitude bits) 0011 + 1001 = 1100 3 + (-1) = -4 CS 230 - Spring 2020 1-65
One’s Complement (BAD) Negative numbers are inverted positive numbers Problems Still two zeros Addition sort-of works Have to re-add in the carry out at the end CS 230 - Spring 2020 1-66
Two's Complement (GOOD) Negative numbers are inverted positive numbers and then add 1 Single zero! n − 1 ...2 n − 1 − 1 − 2 Range: Addition just works (drop carry out) Multiplication also works Have to extend input to 2x the bits CS 230 - Spring 2020 1-67
CS 230 - Spring 2020 1-68
CS 230 - Spring 2020 1-69
CS 230 - Spring 2020 1-70
CS 230 - Spring 2020 1-71
CS 230 - Spring 2020 1-72
CS 230 - Spring 2020 1-73
CS 230 - Spring 2020 1-74
CS 230 - Spring 2020 1-75
CS 230 - Spring 2020 1-76
CS 230 - Spring 2020 1-77
CS 230 - Spring 2020 1-78
CS 230 - Spring 2020 1-79
CS 230 - Spring 2020 1-80
CS 230 - Spring 2020 1-81
CS 230 - Spring 2020 1-82
CS 230 - Spring 2020 1-83
CS 230 - Spring 2020 1-84
CS 230 - Spring 2020 1-85
CS 230 - Spring 2020 1-86
CS 230 - Spring 2020 1-87
CS 230 - Spring 2020 1-88
CS 230 - Spring 2020 1-89
CS 230 - Spring 2020 1-90
CS 230 - Spring 2020 1-91
CS 230 - Spring 2020 1-92
CS 230 - Spring 2020 1-93
CS 230 - Spring 2020 1-94
CS 230 - Spring 2020 1-95
CS 230 - Spring 2020 1-96
CS 230 - Spring 2020 1-97
CS 230 - Spring 2020 1-98
CS 230 - Spring 2020 1-99
CS 230 - Spring 2020 1-100
Recommend
More recommend