15-122: Principles of Imperative Computation

advertisement
15-122: Principles of Imperative Computation
Recitation 3
Section C
September 5, 2012
Office Hours
Tuesdays from 7:00 to 9:00 PM, GHC 4122/26
Come with questions about class, homework, life. . .
TA
Carsten
Josh
Jonathan
Kyle
Alex
Nivedita
Bill
Klas
Ryan
Room
GHC 6505
GHC 4102
GHC 5709
GHC 4122/26
”
”
”
”
”
Time
Thu 1:00 - 3:00
Wed 6:00 - 8:00
Wed 1:30 - 2:30, Fri 1:30 - 2:30
Fri 2:30-4:30
Tue 4:30-5:30, Thurs 4:30-5:30
Wed 3:30-4:30, Fri 3:30 - 4:30
Tue 3:00 - 4:00, Thu 3:00 - 4:00
Thu 7:30 - 9:30
Mon 3:00 - 4:00
CA
Arjun
Elliot
Kathy
Ling-Yi
Maria
Room
GHC 5201
”
”
”
”
Time
Mon 6:00 - 8:00
Sun 6:00 - 8:00
Thu 10:00 - midnight
Wed 8:30 - 10:30
Tue 6:00 -8:00
Binary Representation
Our familiar numerical system is known as base-10. 10 unique digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
348210 = (3 × 103 ) + (4 × 102 ) + (8 × 101 ) + (2 × 100 )
Computers use a base-2 numeral system. 2 unique digits: 0, 1
2710 = 110112 = (1 × 24 ) + (1 × 23 ) + (0 × 22 ) + (1 × 21 ) + (1 × 20 )
Conversion - Decimal to Binary:
Let n be a number in base-10 with binary representation nk nk−1 . . . n1 n0 .
Method 1: Division with remainder
i
0
1
···
k−1
k
n
2i
Method 2: Power Table
2k
nk
remainder
n0
n1
···
nk−1
nk
n
n
2
···
n
2k−1
n
2k
2k−1
···
i
···
20
Test
2k ≤ n
nk 2k + 2k−1 ≤ n
nk−1
..
.
···
ni
nk
..
2k
+ nk−1
.
n0
(
2k−1
+ · · · + 2i ≤ n
···
i
0
i=1 ni 2 ) + 2 = n
Pk
Modular Arithmetic
Suppose we have 4-bit integers. Then there are 24 = 16 numbers we can represent: 0000, 0001, . . . , 1110, 1111. That’s
the interval [0, 15]. Let’s do some simple math:
+
1
0
1
0
1
1
1
0
1
0
1
1
(10)
(5)
(15)
-
01
10 1
10
0
0
1
1
1
1
1
1
0
(13)
(7) Awesome. Things seem to work as expected.
(6)
But what if our operations go outside [0, 15]?
11
+
1
0
0
1
1
0
0
1
1
1
0
1
(13)
(6) We’re dealing with 4-bit ints; there’s no room for the fifth bit.
(19)
11
So we just chop it off and look at the 4 lowest bits. +
(1)
0
0
1
1
0
0
1
1
1
0
1
(13)
(6)
(3)
We’re working mod 24 , so our values can’t be greater than 15. In general, if you have k bits, the highest number you
can represent is 2k − 1.
The following properties are true for arithmetic on integers modulo some number n:
Addition
Multiplication
x+y =y+x
x∗y =y∗x
(commutativity)
(x + y) + z = x + (y + z) (x*y)*z = x*(y*z)
(associativity)
x+0=x
x∗1=x
(application of identity)
x + (−x) = 0 (additive inverse)
x ∗ (y + z) = x ∗ y + x ∗ z (distributivity)
−(−x) = x (cancellation)
x ∗ 0 = 0 (annihilation)
2’s Complement Arithmetic
Looking again at our 4-bit numbers, we decided that they span [0, 15]. But what about negative numbers?
Let’s look at the 4 bits a little differently. Let the most significant (leftmost) bit carry a negative weight.
Then n3 n2 n1 n0 = (n3 ∗ (−1) ∗ 23 ) + (n2 ∗ 22 ) + (n1 ∗ 21 ) + (n0 ∗ 20 ). What are the numbers we have?
0000 (0)
1111 (-1)
0001 (1)
1110 (-2)
0010 (2)
1101 (-3)
0011 (3)
1100 (-4)
0100 (4)
1011 (-5)
0101 (5)
1010 (-6)
0110 (6)
1001 (-7)
0111 (7)
1000 (-8)
Things to note:
1. -0 = 0 : we didn’t lose this property when changing the way we interpret things.
2. min int = -8, max int = 7 : the min negative number has a greater magnitude than the max positive number
3. −x = (∼ x) + 1 : To get −x from x, you flip all the bits of x and then add 1.
4. −min int = min int : the negative of the minimum expressible number is itself.
Hexadecimal
Back to representations - hexadecimal, or base-16, is another numerical system computers use.
16 unique digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f
If n = nk nk−1 . . . n1 n0 is a hex number, then n = (nk × 16k ) + (nk−1 × 16k−1 ) + . . . + (n1 × 161 ) + (n0 × 160 )
16 is kind of a clunky number to work with. Thankfully, we’ve got a nicer way of looking at these hex numbers. Each
hex digit can range from 0 to 16. That’s the span of 4 bits. Let’s explore that idea:
hex
2
3
d
1
9
4
f
6
binary 0010 0011 1101 0001 1001 0100 1111 0110
Then 23d194f616 = 001000111101000110010100111101102
Download