Introduction to Computer Science:
Assignment 4
    Sewickley Academy, 2000-2001

See Course Home Page.  Also, see the Solutions to Assignment 4.

Note:  you may not work in teams on this assignment.
 
Date Assigned: Mon Sep-11
Date Due: Tue Sep-12 by start of class

0a.  Read and study the online Solutions to Assignment 3, including the Extra Credit questions.  This is especially important if you did not receive 100% on the assignment.

0b.  Read Brookshear Chapter 1.4.

1.  Converting binary to decimal.  The task here is to convert 8-bit binary numbers into their decimal equivalents.  We will restrict ourselves to positive numbers, so the sign bit (the leftmost bit) will always be 0.  The remaining 7 bits represent the powers of 2 as follows:
 
Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
sign bit (0) 26=64 25=32 24=16 23=8 22=4 21=2 20=1

We can use this table to easily compute the decimal value for positive 8-bit binary numbers.  Consider the number 01000110.  Which bits have a 1?  Bit 1 (second from the right), Bit 2 (third from the right), and Bit 6 (7th from the right and second from the left).  Looking them up in the table, these correspond to the values 21=2, 22=4, and 26=64.  Thus, 01000110 equals 2 + 4 + 64, or 70.  While it's much easier to read the number 70 in decimal than in binary, it's important to understand that the number 70 is actually stored in the computer as the bits 01000110.

Your task here is to convert the following numbers from binary into decimal, and to neatly show your work:

a.  01000001
b.  00100101
c.  01111111
d.  00000000

2.  Storing Numbers in Flip-Flops (or, Converting decimal to binary).  Now we go the other way around:  given a decimal number between 0 and 127, how do we convert it to an 8-bit binary number.  This direction is a tad more cumbersome, but still straightforward.  Here is the process for writing down all 8 bits from left to right (and note that you must do all the steps, even if the remaining number is 0):

1.  Write down a 0 for the sign bit (we are only working with positive numbers now)
2.  If the number is at least 26=64, write down a 1 and subtract 64 from it, else write down a 0.
3.  If the remaining number is at least 25=32, write down a 1 and subtract 32 from it, else write down a 0.
4.  If the remaining number is at least 24=16, write down a 1 and subtract 16 from it, else write down a 0.
5.  If the remaining number is at least 23=8, write down a 1 and subtract 8 from it, else write down a 0.
6.  If the remaining number is at least 22=4, write down a 1 and subtract 4 from it, else write down a 0.
7.  If the remaining number is at least 21=2, write down a 1 and subtract 2 from it, else write down a 0.
8.  If the remaining number is at least 20=1, write down a 1 and subtract 1 from it, else write down a 0.
9.  The remaining number is 0, and you have written down its 8-bit binary equivalent.  Congratulations!

(By the way:  you should be able to detect a pattern in these steps, so the process is easy to memorize.)

Let's do an example.  Consider the number 42.
 
Step Comment Answer So Far
1 Write down a 0 for the sign bit. 0
2 42 < 64, so we write down 0 and continue.  00
3 42 >= 32, so we write down a 1 and
subtract 32 from 42 to get 10, and continue
001
4 10 < 16, so we write down 0 and continue. 0010
5 10 >= 8, so we write down a 1 and
subtract 8 from 10 to get 2, and continue
0010 1
6 2 < 4, so we write down a 0 and continue. 0010 10
7 2 >= 2, so we write down a 1 and
subtract 2 from 2 to get 0, and continue.
0010 101
8 0 < 1, so we write down a 0 and continue. 0010 1010
9 Indeed, we have 0 remaining.  WE DID IT!!!  0010 1010

Note that we actually include a space every 4 bits for readability, so our final answer is 0010 1010.  So we see that converting 42 from decimal to binary gives us 0010 1010, which we can actually store in 8 flip-flops.  This is essentially how your computer stores the number 42.  Pretty neat.

Your task now:  convert the following numbers from decimal into binary, and neatly show your work.  Hint:  Remember to include all 8 bits, even if this requires some extra "leading 0's" in front of your answer.

a.  64
b.  63
c.  125
d.  0

3.  Storing Text in Flip-Flops (or, Converting Text to Binary).  Here we use the ASCII code to convert text to binary.  To do this, we first convert each symbol to its ASCII code, then we convert those codes to binary using the Decimal-to-Binary process we just covered in problem 2.  Please do not skip the first step (the decimal codes) when showing your work, even though you could do so by using the table in Brookshear Appendix A which maps text to binary codes (reason:  I find this table to be counterproductive, as it is relatively easy (with practice) to remember that 'A' is 65, but who can remember that 'A' is 00100010?).  To work with ASCII, you may need to refer to the following table of ASCII codes:
 
Punctuation:   Digits:   UPPER-CASE
LETTERS:
  lower-case
letters:
 
Text Code Text Code Text Code Text Code
<space> 32 0 48 A 65 a 97
! 33 1 49 B 66 b 98
" 34 2 50 C 67 c 99
# 35 3 51 D 68 d 100
$ 36 4 52 E 69 e 101
% 37 5 53 F 70 f 102
& 38 6 54 G 71 g 103
' 39 7 55 H 72 h 104
( 40 8 56 I 73 i 105
) 41 9 57 J 74 j 106
* 42     K 75 k 107
+ 43     L 76 l 108
. 46     ... ... ... ...
= 61     Y 89 y 121
? 63     Z 90 z 122

So, let's convert the text Go! to binary.  First, we convert each symbol to its ASCII code from the table above, then we convert these codes into binary like in problem 2.  This gives us:
 
Symbol ASCII code Binary
G 71 01000111
o 111 01101111
! 33 00100001

Now we just write down these binary numbers in sequence, being sure to leave a space after every 4 bits for readability.  So, our answer is 0100 0111 0110 1111 0010 0001, which we can actually store in 24 flip-flops.  This is essentially how your computer stores the text Go!.  Once again, pretty neat.

This task:  convert the following text into ASCII and then into binary, and neatly show your work.  Hint:  Remember to include all 8 bits for each ASCII code, even if this requires some extra "leading 0's" in front of your answer.

a.  Wow
b.  2+2 = ?     (note:  there are spaces before and after the = sign)
c.  (#)
 

4.  In an admittedly simplistic effort to guarantee that you at least look at the posted solutions (though you should study them, of course), please answer the following pertaining to the online solutions to Assignment 2:

a.  What color are the gates in the answers to 2 and 3?
b.  The figure for problem 4 was drawn using what standard Windows accessory?
c.  Just below the final figure are 3 letters.  What are they?

5.  Extra Credit (Not Required):  Read the portion of Brookshear 1.6 on Two's Complement Notation (pp. 47-52, not including the part on Excess Notation).  It is worth noting that most computer architectures today actually use Two's Complement Notation to store integers.  Now, write the following decimal numbers in 8-bit Two's Complement Notation (so there is one sign bit and seven magnitude bits), indicating which numbers cannot be written with so few bits:

a.  101
b.  -101
c.  139
d.  0
e.  -1

See Course Home Page.