Professional Documents
Culture Documents
Zeph Grunschlag
Announcements
Midterm coming up on Monday Practice midterms and study guide available from the Homework page. Will add last semesters midterm 1 as well after class. Midterm Review: Probably Sunday night. Lookout for announcement. MIDTERM 2 DATE: Considering moving it from Monday 4/8 to Wednesday 4/10. Please tell me ASAP if this is problematic.
L11 2
Agenda
Euclidean Algorithm for GCD Number Systems
Decimal numbers (base-10) Binary numbers (base-2)
Ones complement Twos complement
Arithmetic Algorithms
Addition Multiplication Subtraction 1s and 2s complement
L11 3
Euclidean Algorithm
m,n
Euclidean Algorithm
gcd(m,n)
integer euclid(pos. integer m, pos. integer n) x = m, y = n while(y > 0) r = x mod y x=y y=r return x
L11 4
r = x mod y
-
x
33
y
77
L11
r = x mod y
-
x
33 77
y
77 33
33 mod 77 = 33
L11
r = x mod y
-
x
33 77 33
y
77 33 11
33 mod 77 = 33 77 mod 33 = 11
L11
r = x mod y
-
x
33 77 33 11
y
77 33 11 0
8
r = x mod y
-
x
244
y
117
L11
r = x mod y
-
x
244 117
y
117 10
L11
10
r = x mod y
-
x
244 117 10
y
117 10 7
L11
11
r = x mod y
-
x
244 117 10 7
y
117 10 7 3
L11
12
r = x mod y
-
x
244 117 10 7 3
y
117 10 7 3 1
L11
13
r = x mod y
-
x
244 117 10 7 3 1
y
117 10 7 3 1 0
14
L11
16
y>x y! x
L11
Number Systems
Q: What does the string of symbols 2134 really mean as a number and why?
L11
22
Number Systems
A: 2 thousands 1 hundreds 3 tens and 4 = 2 103 + 1 102 + 3 101 + 4 100 But on the planet Ogg, the intelligent life forms have only one arm with 5 fingers.
L11
23
Number Systems
So on Ogg, numbers are counted base 5. I.e. on Ogg 2134 means: 2 53 + 1 52 + 3 51 + 4 50 To distinguish between these systems, subscripts are used: (2134)10 for Earth (2134)5 for Ogg
L11
24
Number Systems
DEF: A base b number is a string of symbols u = ak ak-1 ak-2 a2 a1 a0 With the ai taken from {0,1,2,3,,b-2,b-1}. The string of symbols u represents the number (u )b = ak bk + ak-1 bk-1 + . . . + a1 b + a0 NOTE: When b > 10, run out of decimal number symbols so after 7, 8, 9 use capital letters, starting from A, B, C, EG: base-2 (binary) 101, 00010 base-8 (octal ) 74, 0472 base-16 (hexadecimal ) 12F, ABCD L11 25 Q: Compute the base 10 version of these.
Number Systems
A: base-2 (binary) 101, 00010 (101)2 = 1 22 + 0 21 + 1 20 = 5 (00010)2 = 0(24+23+22+20) + 121 = 2 base-8 (octal ) 74, 0472 (74)8 = 7 81 + 4 80 = 60 (0472)8 = 4 82 + 7 81 + 2 80 = 314 base-16 (hexadecimal ) 12F, ABCD (12F)16 = 1162+2161+15160 = 303 (ABCD)16=10163+11162+12161+13160 L11 26 = 43981
Number Systems
The binary and hexadecimal systems are especially important in computer science since binary is the most natural system for bit-strings and hexadecimal for bytestrings (1 byte = 2 hexadecimals) EG in HTML: <font color="ff00ff"> Nice Color </font> Q: What color will this become?
L11 27
Number Systems
A: "ff00ff" represents the rgb value: The first byte is for redness, the second byte is for green-ness, and the last for blue-ness. The HTML command above specifies that there should be 1516 + 15 = 255 redness and blueness values, but no green-ness at all. 255 is the biggest possible byte value so this is bright purple.
L11 28
L11
31
L11
32
L11
33
L11
34
L11
35
k n mod 3 = ui mod 3 i =0
L11
37
Arithmetical Algorithms
Lets write down some familiar arithmetical algorithms. Conveniently, they run the same in any number base. In some cases, such as addition, there are asymptotically faster approaches, but these are the simplest procedures and tend to be fastest for relatively small (e.g. < 1000 bits) number sizes.
L11 38
base-16
x
+y
L11
7 + 1 2 0
4 9 3
6 0 7
3 9 2 + 1
A C 6
4 B F
F 0 F
0 9 9
39
L11
40
1s Complement 2s Complement
The binary number system makes some operations especially simple and efficient under certain representations. Two such representations are
1s complement 2s complement
Each makes subtraction much simpler. Each has disadvantage that number length is pre-determined. Whats actually done in practice.
L11 41
1s Complement
Fix k bits. (EG, k = 8 for bytes) Represent numbers with |x | < 2k-1 Most significant bit tells the sign
0 positive 1 negative
Positive numbers the same as standard binary expansion Negative numbers gotten by taking the boolean complement, hence nomenclature
L11
42
1s Complement Examples
k = 8: 00010010 represents 18 11101101 represents -18 Notice that adding these two representations as usual results in the number 11111111, which by definition is negative 00000000 or 0. This leads to: Guess: adding numbers with mixed sign is the same as adding positive numbers Unfortunate: 0 is not represented uniquely
L11 43
1s Complement Addition
Addition is the same as usual binary addition except: if the final carry is 1, cycle the carry to the least significant digit: 00010010 represents 18, 11110011 represents -12 Sum 00000110 represents 6: Carry: x +y pre-sum overflow answer L11
0 1
0 1
0 1
1 1
0 0
0 0
1 1
0 1
44
1s Complement Addition
Addition is the same as usual binary addition except: if the final carry is 1, cycle the carry to the least significant digit: 00010010 represents 18, 11110011 represents -12 Sum 00000110 represents 6: Carry: x +y pre-sum overflow answer L11
0 1
0 1
0 1
1 1
0 0
0 0
1 1
0 1 1
45
1s Complement Addition
Addition is the same as usual binary addition except: if the final carry is 1, cycle the carry to the least significant digit: 00010010 represents 18, 11110011 represents -12 Sum 00000110 represents 6: Carry: x +y pre-sum overflow answer L11
1
0 1
0 1
0 1
1 1
0 0
0 0
1 1 0
0 1 1
46
1s Complement Addition
Addition is the same as usual binary addition except: if the final carry is 1, cycle the carry to the least significant digit: 00010010 represents 18, 11110011 represents -12 Sum 00000110 represents 6: Carry: x +y pre-sum overflow answer L11
1
0 1
0 1
0 1
1 1
0 0
0 0 1
1 1 0
0 1 1
47
1s Complement Addition
Addition is the same as usual binary addition except: if the final carry is 1, cycle the carry to the least significant digit: 00010010 represents 18, 11110011 represents -12 Sum 00000110 represents 6: Carry: x +y pre-sum overflow answer L11
1
0 1
0 1
0 1
1 1
0 0 0
0 0 1
1 1 0
0 1 1
48
1s Complement Addition
Addition is the same as usual binary addition except: if the final carry is 1, cycle the carry to the least significant digit: 00010010 represents 18, 11110011 represents -12 Sum 00000110 represents 6: Carry: x +y pre-sum overflow answer L11
1 1
0 1
0 1
0 1
1 1 0
0 0 0
0 0 1
1 1 0
0 1 1
49
1s Complement Addition
Addition is the same as usual binary addition except: if the final carry is 1, cycle the carry to the least significant digit: 00010010 represents 18, 11110011 represents -12 Sum 00000110 represents 6: Carry: x +y pre-sum overflow answer L11
1 1 1
0 1
0 1
0 1 0
1 1 0
0 0 0
0 0 1
1 1 0
0 1 1
50
1s Complement Addition
Addition is the same as usual binary addition except: if the final carry is 1, cycle the carry to the least significant digit: 00010010 represents 18, 11110011 represents -12 Sum 00000110 represents 6: Carry: x +y pre-sum overflow answer L11
1 1 1 1
0 1
0 1 0
0 1 0
1 1 0
0 0 0
0 0 1
1 1 0
0 1 1
51
1s Complement Addition
Addition is the same as usual binary addition except: if the final carry is 1, cycle the carry to the least significant digit: 00010010 represents 18, 11110011 represents -12 Sum 00000110 represents 6: Carry: x +y pre-sum overflow answer L11
1 1 1 1
0 1 0
0 1 0
0 1 0
1 1 0
0 0 0
0 0 1
1 1 0
0 1 1
52
1s Complement Addition
Addition is the same as usual binary addition except: if the final carry is 1, cycle the carry to the least significant digit: 00010010 represents 18, 11110011 represents -12 Sum 00000110 represents 6: Carry: x +y pre-sum overflow answer L11
1 1 1 1
0 1 0
0 1 0
0 1 0
1 1 0
0 0 0
0 0 1
1 1 0
0 1 1
1
53
1s Complement Addition
Addition is the same as usual binary addition except: if the final carry is 1, cycle the carry to the least significant digit: 00010010 represents 18, 11110011 represents -12 Sum 00000110 represents 6: Carry: x +y pre-sum overflow answer L11
1 1 1 1
0 1 0
0 1 0
0 1 0
1 1 0
0 0 0
0 0 1
1 1 0
1
0 1 1
1
54
1s Complement Addition
Addition is the same as usual binary addition except: if the final carry is 1, cycle the carry to the least significant digit: 00010010 represents 18, 11110011 represents -12 Sum 00000110 represents 6: Carry: x +y pre-sum overflow answer L11
1 1 1 1
0 1 0
0 1 0
0 1 0
1 1 0
0 0 0
0 0 1
1 1 0
1
0 1 1
1
55
1s Complement Addition
Addition is the same as usual binary addition except: if the final carry is 1, cycle the carry to the least significant digit: 00010010 represents 18, 11110011 represents -12 Sum 00000110 represents 6: Carry: x +y pre-sum overflow answer L11
1 1 1 1
0 1 0
0 1 0
0 1 0
1 1 0
0 0 0
0 0 1 1
1 1 0
1
0 1 1
1
56
1s Complement Addition
Addition is the same as usual binary addition except: if the final carry is 1, cycle the carry to the least significant digit: 00010010 represents 18, 11110011 represents -12 Sum 00000110 represents 6: Carry: x +y pre-sum overflow answer L11
1 1 1 1
0 1 0 0
0 1 0 0
0 1 0 0
1 1 0 0
0 0 0 0
0 0 1 1
1 1 0
1
0 1 1
1
57
2s Complement
Fixes the non-uniqueness of zero problem Adding mixed signs still easy No cycle overflow Javas approach (under the hood) Same fixed length k , sign convention, and definition of positive numbers as with 1s complement Represent numbers with -2(k-1) x < 2(k-1)
EG. Javas byte ranges from -128 to +127
L11 58
2s Complement
Negative numbers: 1. Compute 1s complement 2. Add 1
Not as simple as negating 1s complement
Summarize: -x = x + 1. EG: 00010010 represents 18, 11101101 + 1 = 11101110 represents -18. Add together: 00000000 (over-flow is lost). Q: What are the ranges of Javas 32-bit int and 64-bit long? (All of Javas integer types use 2s complement)
L11 59
2s Complement
A:
2s Complement Addition
Addition is the same as usual binary addition no exceptions!! 11101110 represents -18, 11110100 represents -12 Sum 11111010 represents -30:
1 Carry: x 1 1 1 1 1
+y
1 1
1 1 1
1 1 1
0 1 0
1 0 0
1 1 0
1 0 1
0 0 0
As a final check take the negative to see if get 30: (11100010+1) = (00011101+1) = 00011110. YES!
L11 61
1 1
0 0
1 0
1 1
L11
62
1 1 1
0 0 0
1 0 1
1 1 1
L11
63
1 1 1 0
0 0 0 0
1 0 1 0
1 1 1
L11
64
0 0
1 1 1 0 0
0 0 0 0 0
1 0 1 0
1 1 1
L11
65
0 0
0 0 1
1 1 1 0 0 1
0 0 0 0 0
1 0 1 0
1 1 1
L11
66
1 1
0 0 1
0 0 1 0
1 1 1 0 0 1 0
0 0 0 0 0 0
1 0 1 0
1 1 1
1
67
L11
L11
68