Assignment #1 - Data Lab: Manipulating Bits

Due by 11:59pm, Friday, February 6.

Assignment Overview:

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 Maria Janczak ( If you run into any questions with the assignment, please email her or visit her at any of the office hours below:

The office hours are held at Hylan 301.

Getting Started:

We are providing you with a skeleton code file and testing utilities. To obtain these, copy the file /u/cs252/labs15/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".

Assignment Details:

This section describes the puzzles that you will be solving in bits.c.

Bit manipulations

Function Description Rating Max Ops
oddBits() Return word with all odd-numbered bits set to 1. 1 8
bitXor(x,y) (x^y) using only ~ and | 2 14
swapBytes(x) Swap the positions of the first and last bytes of x (without moving the middle two bytes). 3 25
rotateRight(x,n) Rotate x to the right by n. 3 25
evenBitsCount(x) Returns count of number of 1’s in the even-numbered bits of word. 4 40

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
tmax() Return maximum two’s complement integer. 1 4
subOK(x,y) Determine if can compute x-y without overflow. 3 20
rempwr2(x,n) Compute x%(2n), for 0 <= n <= 30. 3 20
satMul2(x) Multiplies by 2, saturating to Tmin or Tmax if overflow. 3 20
isGreaterOrEqual(x,y) If x>=y then return 1, else return 0. 3 24
multSevenSixteenths(x) Multiplies by 7/16 rounding toward 0. 3 12
isNonZero(x) Check whether x is nonzero using the legal operators except !. 4 10

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. Your code should perform the bit manipulations that implement the specified floating point operations.

Function Description Rating Max Ops
float_abs(uf) Return bit-level equivalent of absolute value of f. 2 10
float_f2i(uf) Return bit-level equivalent of expression (int) f. 4 30
float_quarter(uf) Return bit-level equivalent of expression 0.25*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.


We have included some tools in the handout directory --- btest, dlc,, and fshow --- to help you check the correctness of your work.


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 through Blackboard.

Grading Guideline:

Your score will be computed out of a maximum of 78 points based on the following distribution: 43 points for correctness; 30 points for performance; 5 points for style.

Late Turn-in Policy:

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.