Due by 11:59pm, Monday, February 6.
The purpose of this assignment is to become more familiar with bit-level representations of integers and floating point numbers. You'll do this by solving a series of programming "puzzles". Many of these puzzles are quite artificial, but you'll find yourself thinking much more about bits in working your way through them. This is an individual project, so each person should turn in his/her own.
The managing TA for this assignment is Ashker Mujib (amujib@cs.rochester.edu). His office hours for this assignment are:
We are providing you with a skeleton code file and testing utilities.
To obtain these, copy the file /u/cs252/labs2012/datalab-handout.tar
to a (protected) directory in which you plan to do your work. Then type the
command "tar xvf datalab-handout.tar
".
This will cause a number of files to be unpacked in the directory.
The only file you will be modifying and turning in is bits.c
.
All other files should be left as-is.
The bits.c
file contains a skeleton for each of the 15
programming puzzles. Your assignment is to complete each function
skeleton using only straightline
code for the integer puzzles
(i.e., no loops or conditionals) and a limited number of C arithmetic
and logical operators. Specifically, you are only
allowed to use
the following eight operators:
! ~ & ^ | + << >>A few of the functions further restrict this list. Also, you are not allowed to use any constants longer than 8 bits. See the comments in
bits.c
for detailed rules and a discussion of the desired coding
style.
The file btest.c
allows
you to evaluate the functional correctness of your code. The file
README
contains additional documentation about btest
.
Use the command "make btest
" to generate the test code and run it
with the command "./btest
".
This section describes the puzzles that you will be solving in bits.c
.
Bit manipulations
Function | Description | Rating | Max Ops |
---|---|---|---|
bitAnd(x,y) |
(x&y) using only ~ and | |
1 | 8 |
byteSwap(x,n,m) |
Swap the n th byte and the m th byte. |
2 | 25 |
logicalShift(x,n) |
Shift right logical. | 3 | 20 |
bitParity(x) |
Return 1 if x contains odd number of 0’s. |
4 | 20 |
logicalNeg(x) |
Compute the ! operator without using ! |
4 | 12 |
The table above describes a set of functions that manipulate and
test sets of bits. The "Rating" field gives the difficulty rating
(the number of points) for the puzzle, and the "Max ops" field gives
the maximum number of operators you are allowed to use to implement
each function. See the comments in bits.c
for more details on
the desired behavior of the functions. You may also refer to the test
functions in tests.c
. These are used as reference functions to
express the correct behavior of your functions, although they don't
satisfy the coding rules for your functions.
Two's complement arithmetic
Function | Description | Rating | Max Ops |
---|---|---|---|
tmin() |
Most negative two’s complement integer. | 1 | 4 |
sign(x) |
Is x positive, zero, or negative? |
2 | 10 |
divpwr2(x,n) |
Compute x /2n . |
2 | 15 |
negate(x) |
-x without negation. |
2 | 5 |
isPositive(x) |
x > 0 ? |
3 | 8 |
isLessOrEqual(x,y) |
x <= y ? |
3 | 24 |
ilog2(x) |
Compute the floor of log2(x) . |
4 | 90 |
The table above describes a set of functions that make use of the
two's complement representation of integers. Again, refer to the
comments in bits.c
and the reference versions in tests.c
for more information.
Floating point operations
For this part of the assignment, you will implement some common
single-precision floating-point operations. In this section, you are
allowed to use standard control structures (conditionals, loops), and
you may use both int
and unsigned
data types, including
arbitrary unsigned and integer constants. You may not use any unions,
structs, or arrays. Most significantly, you may not use any floating
point data types, operations, or constants. Instead, any
floating-point operand will be passed to the function as having type
unsigned
, and any returned floating-point value will be of type
unsigned
. Your code should perform the bit manipulations that
implement the specified floating point operations.
Function | Description | Rating | Max Ops |
---|---|---|---|
float_neg(uf) |
Compute -f . |
2 | 10 |
float_i2f(x) |
Compute (float) x . |
4 | 30 |
float_twice(uf) |
Compute 2*f . |
4 | 30 |
The table above describes a set of functions that operate on the bit-level
representations of floating-point numbers. Refer to the comments in
bits.c
and the reference versions in tests.c
for more information. Functions float_neg
and
float_twice
must handle the full range of possible argument
values, including not-a-number (NaN) and infinity. The IEEE standard does
not specify precisely how to handle NaN's, and the IA32 behavior is a bit
obscure. We will follow a convention that for any function returning a
NaN value, it will return the one with bit representation 0x7FC00000
.
We have included some tools in the handout directory --- btest
,
dlc
, driver.pl
, and fshow
--- to help you
check the correctness of your work.
bits.c
. Notice that you must rebuild
btest each time you modify your bits.c
file.
You'll find it helpful to work through the functions one at a time,
testing each one as you go. You can use the -f
flag to instruct
btest
to test only a single function:
"./btest -f bitAnd
".
./dlc -e bits.c
".
Type "./dlc -help
" for a list of command line options.
Don't include the stdio.h
header file in your bits.c
file, as it confuses dlc
and results in some non-intuitive error
messages.
Further, the dlc
program enforces a stricter form of C declarations
than is the case for C++ or that is enforced by gcc
. In
particular, any declaration must appear in a block (what you enclose
in curly braces) before any statement that is not a declaration. For example,
it will complain about the following code:
int foo(int x) { int a = x; a *= 3; /* Statement that is not a declaration */ int b = a; /* ERROR: Declaration not allowed here */ }
btest
and dlc
to compute the correctness and performance points for
your solution. It takes no arguments by running "./driver.pl
".
The TA will use driver.pl
to evaluate your solution.
fshow
to see what
an arbitrary pattern represents as a floating-point number, such as
"./fshow 2080374784
".
You are asked to turn in your solution in a single file bits.c
.
Please include your full name and email address at the top of bits.c
.
Please include any appropriate comments on your code as C comments in bits.c
.
There is no separate README
file for this assignment's turn-in.
You should electronically turn in the required files. Instructions for electronic turn-in can be found on the course web site.
Your score will be computed out of a maximum of 76 points based on the following distribution: 41 points for correctness; 30 points for performance; 5 points for style.
btest
program. You will get full credit for a puzzle if it passes all of the
tests performed by btest
, and no credit otherwise.
Late turn-ins will be accepted for up to three days, with 10% penalty for each late day. No turn-ins more than three-day late will be accepted.