| CPSC 220 | Introduction to Computer Architecture | Fall 2008 |
| Decimal: | Binary: |
| 0 | 0 |
| 1 | 1 |
| 2 | 10 |
| 3 | 11 |
| 4 | 100 |
| 5 | 101 |
| 6 | 110 |
| 7 | 111 |
| 8 | 1000 |
| 9 | 1001 |
| ... | ... |
As you case see, a binary number is then similar to a decimal number in that it is a sequence of digits. In the case of binary numbers, each digit must be 0 or 1 rather than 0-9. Using the binary numbering scheme above, each place represents a different power of 2 whereas each place in a decimal number represents a different power of 10.
To make this more concrete, let's look at an example. In decimal, the number 103 really means (1*102) + (0*101) + (3*100) or (100 + 0 + 3) or 103. Likewise, the binary number 110 really means (1*22) + (1*21) + (0*20) or (4 + 2 + 0) or 6.
To convert a binary number into decimal is straight forward. We initially set the result of the conversion to 0. Then, for each '1' in the binary number, we add to the result, 2p where p is the current position of the binary digit. The positions of the digits are numbered as follows: the rightmost position is 0, the next position to the left is 1, and so on. Note: this is exactly how we determine the magnitude of a decimal number except that for decimal we use powers of 10 rather than 2. Here is how we could convert 110 (6 in decimal) into a decimal number:
(1 * 22) + (1 * 21) + (0 * 20) = 4 + 2 + 0 = 6
To convert from a (non-negative -- we will ignore negative numbers until lab 2) decimal number to a binary number is slightly more difficult. In this case, we repeatedly divide the decimal number by 2. At each iteration, if the current number is divisible by 2, then we write a 0 into the next position in the binary number, otherwise, we write a 1. We move from the rightmost position towards the leftmost position in the resulting binary number. We stop when the current number is 0. For example, here is how we could convert 6 into a binary number:
| 6 => | 3 => | 1 => | 0 |
| even so 0 | odd so 1 | odd so 1 | stop |
| (rightmost bit) | (middle bit) | (leftmost bit) |
This gives us the binary number '110'. Note: we have to reverse it since we go from rightmost position to leftmost position. Note also: we perform integer division by 2, so 3/2 is actually 1 (not 1.5) and 1/2 is actually 0 (not .5). This same technique will work for any decimal number.
Create a ~/cs220 directory for use throughout this course. Then create a lab01 directory within the ~/cs220 directory for use in this lab. Change to the newly-created lab01 directory. Copy the provided files from the directory /classes/f08/cs220/labs/lab01 to your new lab01 directory.
If you have forgotten how to manipulate files and directories, check out the Using Linux tutorial or lab 1 from CS 124. As a reminder (these commands will not be provided in later labs), here are the commands you need to perform the steps above:
bash$ mkdir ~/cs220 bash$ mkdir ~/cs220/lab01 bash$ cd ~/cs220/lab01 bash$ cp /classes/f08/cs220/labs/lab01/* ~/cs220/lab01/
Note: the rest of this lab assumes you are in your lab01 directory.
The file TextIO.java should now have been copied to your lab01 directory. This file defines some methods for reading data typed by the user into your program. Note: it must reside in the same directory as the programs that use it, so make sure it was actually copied to the lab01 directory. You will only need to use two methods from TextIO in this lab: getln() and getlnInt(). The first reads a String from the user and the second reads an int. See Section 2.4 of Professor Eck's Java Textbook for more details on TextIO.
As another reminder, remember to use good programming style. It is important to make sure that others can easily read and understand your programs.
Any program that you turn in for this course should obey the rules of good programming style. You will lose points for poor style, even if your program works correctly. These rules include:
Put a comment at the beginning of the program that explains what the program does. This comment should explain the purpose of the program. Include your name and the assignment name in this comment.
Use meaningful variable and method names which suggest the role of the variable or method in the program, while avoiding extremely long names. Single-letter names are rarely appropriate.
Usually, you should declare each variable on a separate line and include a comment stating the purpose that the variable serves in your program. When several variables are closely related, it might be OK to declare them on the same line.
Start class names with a capital letter and capitalize the first letter of subsequent words (e.g. HelloWorld or TempConvert). Start variable and method names with a lowercase letter, with capital letters for subsequent words (e.g. lastNumber or isPrime).
Use indentation to show the structure of the program. This means that you should indent methods inside the class, and you should indent the statements inside the method. In general, any statements that follow '{' should be indented. If you use emacs or xemacs as a text editor, pressing tab on any line will indent it properly. (If it doesn't, that is often a sign that you have a syntax error somewhere.)
Do not have lines longer than 80 characters. (80 characters is the standard for how much text can be printed on one line of paper.) The easiest way to make sure your lines aren't too long is to set the width of your editor window to be 80 columns, and then hit return to split any line that wraps onto a second line. You can split a line almost anywhere other than in the middle of an identifier, operator, or string. Good places are typically right after operators or commas.
Use comments to describe any complicated control flow used in your program.
Put a comment above each method describing the functionality of the method, as well as its assumptions, parameters, and return value.
Note: Don't use comments to explain how Java works. Assume that anyone reading a Java source code file knows Java. A comment such as "declare a variable named interestRate" is useless. However, the comment "annual interest rate, expressed in decimal form" gives information that makes the program easier to understand.
Here are the exercises for this week's lab writeup. The writeup is due in lab next Thursday (at the start of lab).
Write a Java program called ConvBinToDec.java for converting a binary number into an (unsigned -- non-negative) int. Your program should first prompt the user for a binary number and then read the binary number in using TextIO. Your program must read the binary number as a String (this is a requirement). It then should scan the characters in the String performing the conversion as discussed in the text above. Finally, it should print out the resulting int. If the String does not contain a binary number (i.e., it is not all 0's and 1's), then your program should print an error message and exit.
Note: you may not use any auxiliary classes or packages besides String and TextIO, including built-in classes such as Integer.
Write a Java program ConvDecToBin.java for converting an (unsigned -- non-negative) int into a binary number. Your program should first prompt the user for a non-negative int and then read in the int using TextIO. If the int is negative, then your program should print an error message and exit. Otherwise, it should convert this number into a binary number, storing the result into a String (you must use a String -- this is a requirement). You can use the technique discussed in the text above. Finally, your program should print out the resulting binary string.
Note: you may not use any auxiliary classes or packages besides String and TextIO, including built-in classes such as Integer.
Verify that your lab01 folder contains all of the files you created or modified for this lab, then copy your entire lab01 folder to the handin directory ~mcorliss/handin/cs220/username (where username is replaced with your username). To copy a whole directory instead of a single file or a collection of files, use
cp -r sourcedir targetdir
where sourcedir is the directory you want to copy and targetdir is where it will be copied to. The "-r" means "recursive" and tells the system to copy the source directory, any files it contains, any subdirectories it contains, any files those subdirectories contain, any subdirectories the subdirectories contain, etc. In this case, the following command will work regardless of what directory you are currently in:
cp -r ~/cs220/lab01 ~mcorliss/handin/cs220/username
Of course, if you named your cs220 directory something other than "cs220", you'll need to substitute your directory's name as appropriate. You can use the ls command (or dir) to check that the directory ~mcorliss/handin/cs220/username contains a lab01 directory, and that the directory ~mcorliss/handin/cs220/username/lab01 contains copies of the files you wanted to hand in:
ls ~mcorliss/handin/cs220/username/lab01
This command should print out the files and directories in your lab01 directory that were copied to the handin folder. At the very least, it should list BinToDecConv.java and DecToBinConv.java.
You can also hand things in multiple times - the cp command will overwrite any existing file with the same name.
You can also delete things if you hand in something you didn't want to. Remove files using the command:
rm filename
where filename is replaced by the name of the file you want to remove (e.g. ~mcorliss/handin/cs220/username/lab01/ConvBinToDec.java to remove the file ConvBinToDec.java from your handin directory). Note: including the full path here is important - if you are in your own lab01 directory and type rm ConvBinToDec.java, you'll delete your copy of the ConvBinToDec.java and will have to redo exercise #1 from scratch.
Another very important note: rm deletes things for real so be careful when you use it - a deleted file is gone for good. (If the file had been in existence for a while before you deleted it, you may be able to recover a version from a system backup. You will lose any changes made to the file since the last backup, however.)
You can delete a directory with the command
rmdir dirname
Note that the directory must be empty of all files before it can be removed.
Finally, you can delete files and directories all together with
rm -r dirname
As with cp, the "-r" means recursive and means that it will delete the directory, all files it contains, all subdirectories it contains, all files contained in the subdirectories, all subdirectories contained in the subdirectories, etc. Warning: rm -r is a very powerful command and should be used with extreme caution.
|
|