CS130 Homework 1 Solution

Binary Numbers

For full credit, show your work!

1. Convert 34 to an 8-bit binary number.
8 bits represent the values 27 to 20 (128, 64, 32, 16, 8, 4, 2, 1)
the largerst we can use is 32
34-32 -2
We can then use the 2
2-2 = 0
So our binary equivalent is: 00100010




2. Convert -68 to an 8-bit binary number (2s complement)

First represent 68 as a binary number:

(64 and 4) => 01000100

flip all the bits:

10111011

Add 1

ANSWER: 10111100


3. Convert the following numbers from their 2s complement binary representation to decimal:

00010110

this is a positive number so we do a direct conversion:
0 128
0 64
0 32
1 16
0 8
1 4
1 2
0 1

ANSWER: 16 + 4 + 2 = 22


10000101
This is a negative number (because of the leading 1)
1 -128
1 4
1 1
ANSWER: -128 + 4 + 1 = -123


4. Add the binary numbers:
    10110011      10011001
    00111010      10001101
    11101101     100100110

5. In problem 4, is there an underflow or overflow situation?  How do you know?

There is an underflow:
a) we added 2 negative numbers and got a positive number
b) the carry in to the sign bit is different than the carry out of the sign bit