Homework 2 Solutions
Problem 1
a. Assume that there are exactly 267 students in your class. If every
student is to be assigned a unique bit pattern, what is the minimum
number of bits required to do this?
b.How many more students can be admitted to the class without
requiring additional bits for each student's unique bit pattern?
- The number of unique bit patterns using i bits is 2i.
We need at least 267 unique bit patterns. The cleanest approach is to
compute log2267 and take the ceiling (round up). This yields
9 as the answer. I accept trial and error is an acceptable
technique.
- With 9 bits, we can represent up to 512 unique bit patterns;
we can represent 512 - 267 = 245 more students without requiring
additional bits.
(4 points.)
Problem 2
Using 7 bits to represent each number, write the representations of 26 and -26 in signed magnitude and
2's complement integers.
|
Signed Magnitude |
2's Complement |
26 |
0011010 |
0011010 |
-26 |
1011010 |
1100110 |
(2 points.)
Problem 3a. What is the largest positive number one can
represent in an 11-bit 2's complement code? Write your result in binary
and decimal.
b. What is the greatest magnitude negative number one can represent in an 11-bit 2's complement code? Write your result in binary
and decimal.
c. What is the largest positive number one can represent in n-bit 2's complement code?
d. What is the greatest magnitude negative number one can represent in n-bit 2's complement code?
- 01111111111 2's complement binary = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 + 256 + 512 = 1023
decimal
- 10000000000 2's complement binary = -(10000000000 + 1) =
-(01111111111 + 1) = -(1023 + 1) = -1024
- 2n-1 - 1
- -2n-1
(4 points.)
Problem 4
What are the 8-bit patterns used to represent each of the
characters in the string "Badgers 20!"? (Only represent the characters
between the quotation marks.)
Using table E.2 from ItCS:
"B" = 0x42 = 01000010
"a" = 0x61 = 01100001
"d" = 0x64 = 01100100
"g" = 0x67 = 01100111
"e" = 0x65 = 01100101
"r" = 0x72 = 01110010
"s" = 0x73 = 01110011
" " = 0x20 = 00100000
"2" = 0x32 = 00110010
"0" = 0x30 = 00110000
"!" = 0x21 = 00100001
(The above, in some form or another, is all that is necessary to get full
credit.)
The entire string is thus 0x4261646765727320323021, which is
0100001001100001
0110010001100111
0110010101110010
0111001100100000
0011001000110000
00100001
in binary. (3 points.)
Problem 5
Convert the following 2's complement binary numbers to decimal.
- 0101
- 1011
- 11100110
- 1000110011111000
- 0101 = 1*4 + 0*2 + 1*1 = 4 + 1 = 5
- 1011 = -(1011 + 1) = -(0100 + 1) = -(4 + 1) = -5
- 01111111 = 64 + 32 + 16 + 8 + 4 + 2 + 1 = 127
- 1100101110011100 = -(1100101110011100 + 1) =
-(0011010001100011 + 1) = -(13411 + 1) = -13412
(4 points.)
Problem 6
Express the negative value -21 as a 2's complement integer, using eight bits. Repeat, using 16 bits. Repeat, using 32
bits. What does this illustrate with respect to the properties of sign extension as they pertain to 2's complement
representation?
First, let's find the 8-bit binary representation of 21:
21 is odd, so 20 coefficient (bit 0) is 1; Subtract 1: 21 - 1 = 20; Divide by 2: 20 / 2 = 10.
10 is even, so 21 coefficient (bit 1) is 0; Subtract 0: 10 - 0 = 10; Divide by 2: 10 / 2 = 5.
5 is odd, so 22 coefficient (bit 2) is 1; Subtract 1: 5 - 1 = 4; Divide by 2: 4 / 2 = 2.
2 is even, so 23 coefficient (bit 3) is 0; Subtract 0: 2 - 0 = 2; Divide by 2: 2 / 2 = 1.
1 is odd, so 24 coefficient (bit 4) is 1; Subtract 1: 1 - 1 = 0; We are done.
All remaining bits (bits 5-7) are 0s.
21 in 8-bit binary is 00010101. For the other bit representations, we just extend the left hand side with 0s. So
21 in 16-bit binary is 0000000000010101 and
21 in 32-bit binary it is 00000000000000000000000000010101
Now we must convert each of these two's complement representations to
the same number represented negatively (-21). We find the negative
representation by taking the complement and adding one:
00010101 + 1 = 11101010 + 1 = 11101011
0000000000010101 + 1 = 1111111111101010 + 1 = 1111111111101011
00000000000000000000000000010101 + 1 = 11111111111111111111111111101010 + 1 = 11111111111111111111111111101011
The pattern that we see is that when we increase the number of bits in the 2's complement representation, we sign extend,
meaning that we take the leftmost bit of the original representation
and duplicate it into the added bits of the new representation. (3 points.)
Problem 7
The following binary numbers are 4-bit 2's
complement binary numbers. Which of the following operations generate
overflow? Justify your
answers by translating the operands and results into decimal.
- 0011 + 1100
- 0111 + 1111
- 1110 + 1000
- 0110 + 0010
- 0011 + 1100 = 1111 <= OK
3 + (-4) = -1
- 0111 + 1111 = 0110 <= OK
7 + (-1) = 6
- 1110 + 1000 = 0110 <= OVERFLOW!
(-2) + (-8) = 6?
- 0110 + 0010 = 1000 <= OVERFLOW!
6 + 2 = (-8)?
(4 points.)
Problem 8
Describe what conditions indicate overflow has occured when two 2's complement numbers are added.
When adding two numbers, overflow occurs when the two operands have the same
leftmost bit and the leftmost bit of the answer is different. When the
operands have differing leftmost bits, overflow cannot occur when adding them
together. The leftmost bit is frequently referred to as the Most Significant
Bit (MSB for short). (2 points.)
Problem 9
Compute the following:
- NOT(1011) OR NOT(1100)
- NOT(1000 AND (1100 OR 0101))
- NOT(1011) OR NOT(1100) = 0100 OR 0011 = 0111
- NOT(1000 AND (1100 OR 0101)) = NOT(1000 AND 1101) = NOT(1000) = 0111
(2 points.)
Problem 10
Write the decimal equivalents for these IEEE floating point numbers.
- 0 01111111 00000000000000000000000
- 1 01111110 10000000000000000000000
- 0 01111111 00000000000000000000000 = (-1)0 * 1.0 * 2127-127 = 1 * 1.0 *
20 = 1 * 1 * 1 = 1
- 1 01111110 10000000000000000000000 = (-1)1 * 1.5 * 2126-127 = -1 * 1.5 * 2-1 = -1 * 1.5 * 0.5
= -0.75
(2 points.)
|