Programming due Saturday, Feb. 8 at 11:59pm, no late submission.
This assignment has four objectives:
- to write a C program that manipulates the bits of integer values,
- to learn the C language operators for binary numbers,
- to build a more complex C program with multiple files,
- to see if you can follow directions!
About The Assignment
This assignment will serve as the basis for future assignments. You will learn how to
use the C language operators for binary
and (
&),
binary
or (
|), and binary
not (
~). You will also use the
C language
bit shift operators (
<< and
>>).
The goal of the assignment is to implement a small C library (set of 3
functions) that enables getting and setting subfields in a binary number.
This is especially useful for playing around with numerical representations,
for example you could build a new floating point number from scratch by setting the sign
bit, exponent, and mantissa, or you could analyze an existing floating point number by
extracting the same fields. We will use it later in this class for understanding number
representations and for converting LC3 assembly code into machine code.
To get started, read the Getting Started section below and then study the
documentation for field.h in the Files tab to understand the details of the assignment.
Getting Started
Perform the following steps
- Create a directory for this assignment. A general scheme might be
to have a directory for each CS class you are taking and beneath that,
a directory for each assignment. The name of the directory is
arbitrary, but you may find it useful to name it for the assignment
(e.g. PA2).
- Copy the three files into this directory. It is easiest to click on the link,
then copy the code using CTRL-A (select), CTRL-C (copy) and CTRL-V (paste) into an editor window.
- Open a terminal and make sure you are in the directory you created
in step 1. The
cd
command can be used for this.
- In the terminal type the following three commands to build the executable.
gcc -g -Wall -c testField.c
gcc -g -Wall -c field.c
gcc -g testField.o field.o -o testField
- In the terminal type
testField
and read how to run the
the program.
- In the terminal type
testField bin 11259375
and you should see the output:
dec: 11259375 hex: 0xABCDEF bin: 0000-0000-1010-1011-1100-1101-1110-1111
You now have a functioning program. All the commands work, however, only bin
will produce correct results at this point.
Completing the Code
Before attempting to write any of the functions of
field.c
,
study the documentation in found in the files tab.
Plan what you need to do
before writing code.
The best way to complete the code is to follow a write/compile/test
sequence. Do not attempt to write everything at once. Rather choose one
function and do the following steps:
- Write one of the functions in
field.c
using your favorite editor.
- Save your changes and recompile
field.c
using gcc -g -Wall -c field.c
.
- You may find it convenient to work with both a terminal and editor window at the same time.
- Repeat steps 1 and 2 until there are no errors or warnings.
- Build the executable using
gcc -g testField.o field.o -o testField
- Test the function you have been working on using the command line.
- Warning: Do not attempt to move on until you complete and thoroughly test a function!
- Repeat steps 1 thru 7 for the remaining functions.
- Relax, you are done with your assignment!
Specifications
Your program must meet the following specifications:
- Work on your own, as always.
- The name of the source code file must be exactly field.c.
- Name the file exactly - upper and lower case matters!
- Comments at the top as shown above.
- Make sure your code runs on machines in the COMCS 120 lab.
- Submit your program to the Checkin tab as you were shown in the recitation.
- Read the syllabus for the late policy.
- We will be checking programs for plagiarism, so please don't copy from anyone else.
Grading Criteria
- 100 points for perfect submission.
- 0 points for no submission, will not compile, submitted class file, etc.
- Each test can make multiple calls to the function being tested, with different values.
- Preliminary Tests
- testCompile: checks that program compiles. (0 points)
- test1: calls testField with get to check the getField function, with isSigned false. (10 points)
- test2: calls testField with get to check the getField function, with isSigned true. (10 points)
- test3: calls testField with set to check the setField function. (10 points)
- test4: calls testField with fits to check the fieldFits function, with isSigned false. (10 points)
- test5: calls testField with fits to check the fieldFits function, with isSigned true. (10 points)
- Final Tests
- test6: further testing of the getField function. (10 points)
- test7: further testing of the setField function. (10 points)
- test8: further testing of the fieldFits function. (10 points)
- test9: Creates a float variable, and calls getField to get the sign, exponent, and mantissa fields. (10 points)
- test10: Creates a float variable, and calls setField to set the sign, exponent, and mantissa. (10 points)
- Final grading includes the preliminary tests.
Submit the single file
field.c
to the Checkin tab on the course website, as you
were shown in the recitation, and read the syllabus for the late policy (if necessary).
© 2014 CS270 Colorado State University. All Rights Reserved.