Homework A/B Solution

My solutions to the homework exercises appear in these boxes below the problem. A couple notes:
  • * means times
  • ^ means exponent or "to the power of"

Enjoy... yow, bill

1. Convert the following binary numbers to octal, hex, and decimal:

To binary to octal, starting from the point and heading left, group each three bits into one octal number. If the number contains a fractional part (bits to the right of the point), then start at the point head right and group 3 bits into an octal number.

Converting binary to hex, is a similar procedure except that bits are grouped 4 at a time, rather than 3.

Converting binary to decimal, sum the value of each bit position whose value is 1. Don't forget that you can check your work by converting your octal and hex answers to decimal and checking them against the binary conversion.

a) 1001

Octal:

11

Hex:

9

Decimal:

9

b) 1001 1010

Octal:

232

Hex:

9A

Decimal:

154

c) 1000 1011.011

Octal:

213.3

Hex:

8B.6

Decimal:

139.375

2. Convert the following decimal numbers to binary, octal and hex:

To convert decimal to binary, find the largest power of 2 that fits into the decimal number, subtract that amount, and then continue with the bits to the right. You should also be able to use the "halving method" described in class. This is particularly useful for large numbers.

To convert to octal or hex, you can use the same procedure described for binary by finding the largest power of 8/16 and so on. A potentially easier approach is to convert to binary first, then do the binary to oct/hex conversion described above.

a) 43

Binary:

10 1011

Octal:

53

Hex:

2B

b) 2005

Binary:

111 1101 0101

Octal:

3725

Hex:

7D5

3. Express the decimal number -2005 in each binary format:

a) signed magnitude

In signed-magnitude representation, a 1 in the first bit turns the following binary number to negative.

SM: 1111 1101 0101

b) 1's complement

In 1's complement, the first (sign) bit is equal to -2^n + 1. Negating a positive binary number is accomplished by switching each bit.

1's comp: 1000 0010 1010

c) 2's complement

In 2's complement, the first (sign) bit is equal to -2^n. Negating a positive binary number is accomplished by switching each bit and then adding 1.

2's comp: 1000 0010 1011

d) Excess 2048

In excess representation, the number is stored as the binary value + 2^(m-1), where m is the number of bits. So, for example, -2005 would be represented as -2005 + 2048 = 43 in binary.

Also, our text points out a shortcut that signed-magnitude is identical to 2's complement with the sign bit reversed.

Excess: 0000 0010 1011

4. Complete problem 6 on page 689

We didn't do this in class. Use each phalange (finger or toe) to represent one bit. Doing this you can: 
  • count from 0 to 31 (2^5 - 1) using 5 fingers
  • count from 0 to 1023 (2^10 - 1) using 10 fingers
  • count from 0 to (2^20 - 1) using 10 fingers and 10 toes, though I'll offer extra credit if you can hold up individual toes ala your fingers.

In doing 2's complement, let's start smaller. Using one hand with your thumb as the sign bit, then the largest negative number you can represent is your thumb up (or 1) followed by four 0's. In 2's complement, the sign bit is equal to -2^(n-1), so that's -2^4 or -16. The largest positive quantity is thumb 0 followed by four 1's, equaling 15.

Extending this to all your fingers and toes:

  • the largest negative quantity is left big toe 1 and the rest 0's. This equals -2^19, or -512K.
  • the largest positive quantity is left big toe 0 and the rest 1's. This equals 2^19 - 1, or 512K - 1.

5. Complete problem 7 on page 689

Remember, you get negate a number in 2's complement by flipping each bit and adding one. When adding 2's complement, we throw away any leftover carry bit, if one exists.
a.
  0010 1101
+ 0110 1111
   ----------------
   1001 1100
b. 
    1111 1111
  1111 1111
   ----------------
   1 1111 1110

(note: this is actually an overflow for a 8-bit quantity)

c. 
  0000 0000
1111 1111
   -------------
 
or, in 2's complement:
 
  0 0000 0000
+ 1 0000 0001
   ----------------
  1 0000 0001
d. 
  1111 0111
1111 0111
   -------------
 
or, in 2's complement:
 
  0 1111 0111
+ 1 0000 1001
   ----------------
  0 0000 0000

6. Complete problem 8 on page 689

You negate a number in 1's complement by flipping each bit. If a carry occurs, then wrap-around and add it to the final result. Also, notice that adding positive quantities is the same in 2's complement and 1's complement.
a. Same as #5a above

 

b. Same as #5b above

 

c. 
  0000 0000
1111 1111
   -------------
 
or, in 1's complement:
 
  0 0000 0000
+ 1 0000 0000
   ----------------
  1 0000 0000
d. 
  1111 0111
1111 0111
   -------------
 
or, in 1's complement:
 
  0 1111 0111
+ 1 0000 1000
   ----------------
  1 1111 1111

(Note: this is "negative zero", and a quantity with all 0's would be a "positive zero")

7. Complete problem 14 on page 690

Binary multiplication is just like decimal multiplication:
  0111
x 0011
  ----
  0111
 0111
------
 10101

Check it in decimal: 7 * 3 = 21. Yup.

... Appendix B questions below...

8. Complete problem 1 on page 698

The single precision format is

   S (1 sign bit), E (8 exponent bits), F (23 fraction bits)

which is assigned a value of

   value = (-1)^S + (1 + F) * 2^(E - 127)

The exponent is represented in biased by 127, hence the subtraction. The fraction is normalized with a 1 assumed before the point.

a. 9

Convert to a normalized binary number:

9 = 1001 * 2^0 = 1.001 * 2^3

Giving:

S = 0 (positive)
E = 1000 0010 (130-127=3 in bias 127)
F = 00100...0 (1.001)
 
  0100 0001 0001 0000 0000 0000 0000 0000
     4    1    1    0    0    0    0    0
hex = 4110 0000
b. 5/32

Convert:

= 101 * 2^-5 = 1.01 * 2^-3

Giving:

S = 0 (positive)
E = 0111 1100 (124-127=-3 in bias 127)
F = 01000...0 (1.01)
 
  0011 1110 0010 0000 0000 0000 0000 0000
     3    E    2    0    0    0    0    0
hex = 3E20 0000
c. -5/32

Just flip the sign bit of the answer above:

S = 1 (negative)
E = 0111 1100
F = 01000...0
 
  1011 1110 0010 0000 0000 0000 0000 0000
     B    E    2    0    0    0    0    0
hex = BE20 0000
d. 6.125

Convert:

= 110.001 * 2^0 = 1.10001 * 2^2

Go:

S = 0 (positive)
E = 1000 0001 (129-127=2 in bias 127)
F = 100010...0 (1.10001)
 
  0100 0000 1100 0100 0000 0000 0000 0000
     4    0    C    4    0    0    0    0
hex = 40C4 0000

9. Complete problem 2 on page 698, convert to binary or hex value rather than decimal to save some time.

a. 42E4 8000

Expand to binary:

0100 0010 1110 0100 1000 0000 0000 0000
  
S = 0 (positive)
E = 1000 0101 (133-127 = 6)
F = 1100 1001 0000 0000 0000 000
  = 1.11001001
 
  = 1.11001001 * 2^6
  = 1110010.01 (binary)
  = 114.25 (decimal)
b. 3F88 0000

Go:

0011 1111 1000 1000 0000 0000 0000 0000
 
S = 0 (positive)
E = 0111 1111 (127-127 = 0)
F = 0001 0000 0000 0000 0000 000
  = 1.0001
 
  = 1.0001 * 2^0
  = 1.0001 (binary)
  = 1 + 1/16 = 1.0625 (decimal)
c. 0080 0000

Go:

0000 0000 1000 0000 0000 0000 0000 0000
 
S = 0 (positive)
E = 0000 0001 (1-127 = -126)
F = 0000 0000 0000 0000 0000 000
  = 1.0
 
  = 1.0 * 2^-126
  = .00...(125 total zeros)...1 (binary)
  = a very small number (decimal)
d. C7F0 0000

Go:

1100 0111 1111 0000 0000 0000 0000 0000
 
S = 1 (negative)
E = 1000 1111 (143-127 = 16)
F = 1110 0000 0000 0000 0000 000
  = 1.111
 
  = 1.111 * 2^16
  = -1 1110 0000 0000 0000 (binary)
  = -1    E    0    0    0 (hex)
  = (1 * 65536) + (14 * 4096)
  = -122,880 (decimal)