hw1solution - Computer Science and Engineering

advertisement
CIS 360 Winter 2004 Homework #1 Solution
Exercises:
1. [3 points] What is meant by the most significant and least significant bit in a decimal
number? Is the meaning similar in a binary number? Explain.
Answer: The most significant “bit” (or more properly digit) in a decimal
number is the digit that has the greatest effect on the value of the number.
Similarly the least significant digit is the digit that has the least effect on the
value of the number. The MSD in decimal is the leftmost digit, since it
represents the largest power of 10. The LSD is the rightmost digit, since it
represents the smallest power of 10. The meaning is similar in binary, since
binary numbers are also positional. The leftmost bit represents the largest
power of 2 and therefore has the greatest effect on the value; the rightmost bit
represents the smallest power of 2 and therefore has the least effect on the
value.
2. [5 points] We understand what the number 101.101 (decimal) represents. What value
does 101.101 (binary) represent? Hint: The formula for converting a number in any base
to decimal was given and it generalizes correctly for solving this problem.
101.101 = 1* 2^2 + 0*2^1 + 1*2^0 + 1*2^-1 + 0*2^-2 + 1*2^-3 =
4 + 0 + 1 + ½ + 0 + 1/8 = 5 5/8 or 5.625
3. [25 points; 5 each] What is 291 (decimal):
a. in base 2? 256 + 32 + 2 + 1 = 100100011
b. in octal? (100 100 011 binary) = 443 octal
c. in hexadecimal? (1 0010 0011 binary) = 123 hexadecimal
d. in base 20? (Note: digits are 0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J )
291 / 20 = 14 r B (11 decimal)
14 /20 = 0 r E (14 decimal)
Answer: EB base 20
e. written using BCD? 0010 1001 0001
4. [15 points; 5 each] What is -75 (decimal):
a. in 8-bit signed magnitude? 11001011 (Note: 75 decimal = 64+8 + 2+ 1 =
01001011 in simple binary)
b. in 8-bit 2's complement? 10110100 + 1 = 10110101
c. written using BCD? 1011 0111 0101
5. [12 points] Given two 4-bit numeric representations: 0101 + 1011, what is the result
(give the binary and decimal) of adding them together if they are:
a. signed magnitude?
Answer: 0101 = +5 decimal; 1011 = -3 decimal so 0101 + 1011 is equivalent
to 0101- 0011 = 0010; which is 2 decimal. This illustrates that binary
subtraction is needed if numbers are stored in signed magnitude.
b. 2's complement?
Answer: 0101 = +5 decimal; 1011 = -5 decimal (The first bit indicates it is a
negative number. I determined its value by flipping all the bits and adding one:
0100+1). Doing the binary addition 0101 + 1011 yields 0000 with a carry-in of 1
and a carry-out of one. Since carry-in equals carry-out, the result is valid, a
decimal 0.
6. [18 points] Show how to add the following numbers (which are decimal) using 5-bit
two's complement. Show the binary numbers you are adding. Also, show the answer in
binary and the answer converted to decimal.
a. 9 + 9 = 01001 + 01001 = 10010 with a carry-in of 1 and carry-out of 0.
Since the carries differ I know there is overflow and the result is
incorrect (-14 instead of the expected 18).
b. -13 + 7 = 10011 + 00111 = 11010 with a carry-in of 0 and a carry-out of 0. I
know the result is correct and is a negative number. I determined its value by
flipping all the bits and adding 1 (00101 + 1 = 00110) giving -6 decimal
c. -10 + -2 = 10110 + 11110 = 10100 = with a carry-in of 1 and a carry-out of 1. I
know the result is correct and is negative. I determined its value by flipping all
the bits and adding 1 (01011 + 1 = 01100) giving -12 decimal
7. [12 points] What is the range (maximum and minimum representable values) of:
a. a 10-bit signed magnitude representation? -511 … 511
b. a 10-bit 2's complement representation? -512 … 511
8. [5 points] Give the least number of binary digits (bits) needed for an unsigned (simple
binary) representation of the decimal number 5,260.
Answer: You could completely convert the number to binary and count the bits,
but it is not necessary to do this if you recognize that 4096 = 2^12 is the highest
power of 2 in 5260. That tells you that the leftmost 1 will be in position 12.
Since positions are numbered starting at zero, 13 bits will be needed.
9. [5 points] A computer manufacturer advertises the memory size of its machines
in MBytes (megabytes). John assumes that he is buying memory in units of
1,000,000 bytes. Mary assumes she is getting memory in units of 1,048,576
bytes. Why is there confusion in how to interpret the abbreviation M? Hint:
Search the web for “SI prefixes”.
See http://physics.nist.gov/cuu/Units/binary.html.
According to the International System of Units, which is the standard for
physics and engineering, M is an abbreviation for 10^6. Historically, computer
scientists have noted that 2^20 = 1,048,576 which is somewhat close, so they
have (among themselves) called 2^20 bits of storage a meg, and used the
abbreviation M. As computers have become ubiquitous, this double meaning
has caused confusion. The preferred meaning now is
one mebibyte 1 MiB = 220 B = 1,048,576 B
one megabyte 1 MB = 106 B = 1,000,000 B
John is (correctly) thinking in decimal, while Mary is (incorrectly) thinking in
binary.
Download