CS442, Spring 2006, HW #2 Answers 1. A OR B 2. (a) the biggest power of 2 smaller than 113 is 64 = 2^6 113 - 64 = 49 the biggest power of 2 smaller than 49 is 32 = 2^5 49 - 32 = 17 the biggest power of 2 smaller than 17 is 16 = 2^4 17 - 16 = 1 113 (base ten) = 1110001 (base 2) = 2^0 + 2^4 + 2^5 + 2^6 (b) 10110010 (base 2) = 2^1 + 2^4 + 2^5 + 2^7 = 178 (base 10) (c) carries: 11 1 10110010 (178) + 00111010 ( 58) -------- 11101100 (236) 3. (a) If we look at the definition of the addc operation and count all the "or"s, "and"s and "not"s, we get: - and : 8 + 8 = 16 - or: 3 + 3 = 6 - not: 6 + 3 = 9 (b) We look at the definition of the addbyte operation, and count each of the different types of operations we have: - addc : 8 = 8 x 16 = 128 "and"s, 8 x 6 = 48 "or"s and 8 x 9 = 72 "not"s 4. if we press the "cycle" button for 7 times, it gets stuck in line 11. If we look in memory location 14 we can see that the value stored there is 0. 5. Depends on the song each one of you chose. 6. The thing is unlocked if the output is 1. We just have to see what the inputs have to be in order for the output to be 1. If we write the logical expresion for the output in terms of the inputs, we get: output = (bit4 and (not bit3)) and (bit2 and (bit1 and (not bit0))) So, both (bit4 and (not bit3)) and (bit2 and (bit1 and (not bit0))) have to be 1 for (bit4 and (not bit3)) to be 1, we need bit4 to be 1 and (not bit3) to be 1, so bit4=1 and bit3=0 . For (bit2 and (bit1 and (not bit0))) to be 1, we need bit2=1, bit1=1 and bit0=1 . So, the key is 10110 = 2+4+16=22 . 7. We can make a table with results for mem[14] depending on what's stored in mem[12] and mem[13] if we set mem[12] to 0 and press the "cycle" button for a few times, we will get mem[14] to be 0, regardless on what's in mem[12] if we set mem[13] to 0, the same thing will happen, regardless on what's in mem[12] . If we set both mem[12] and mem[13] to 1, after pressing "cycle" a few times, we'll get mem[14] to be 1. mem[12] mem[13] mem[14] ------------------------- 0 0 0 0 1 0 1 0 0 1 1 1 This table looks like an AND, so the function that the program computes is mem[14] = mem[12] AND mem[13] . 8. (a) Sorter2 grabs a sock and looks for its match. Sorter5 grabs three socks and puts them all back if no match is found. Although sorter5 is more likely to find a match, I would think that it is more wasteful since it replaces 3 socks each time it *doesn't* find a match. So, I'd guess sorter5 does worse than sorter2. (b) mean 2526.2 max 3941 min 1550 (c) The resutls for sorter2 in class were a mean of 2571.77, max of 3779, and min of 1660. So, the two algorithms produce very similar results with a very different approach. On average, however, sorter5 beats sorter2, so I was wrong!