Assignment 1: Datalab

Introduction

The purpose of this assignment is to become more familiar with representing and manipulating data as bits.  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. 

You are to work individually on this assignment.  Assignments later in the semester may be done in pairs. 

Getting Started

We are providing you with a skeleton code file and testing utilities.  To obtain these, copy the file /u/cs252/labs_2011/datalab-handout.tar or download this to a (protected) directory in which you plan to do your work.  Then type the command:

tar xvf datalab-handout.tar
to the shell.  This will cause twelve files to be created in your directory:
  • README
  • Makefile
  • bits.h
  • btest.h
  • bits.c
  • btest.c
  • decl.c
  • fshow.c
  • ishow.c
  • tests.c
  • driver.pl
  • dlc

The only file you will be modifying and turning in is bits.c.  All other files should be left as-is.  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 

Looking at the file bits.c you’ll notice a C stucture team into which you should insert information identifying yourself.  Do this right away so you don’t forget. 

The bits.c file also contains a skeleton for each of the 15 programming puzzles.  Your assignment is to complete each function skeleton using only straightline code (i.e., no loops or conditionals) and a limited number of C arithmetic and logical operators.  Specifically, you are allowed to use only the following eight operators:

 ! ~ & ^ | + << >> 
A few of the functions further restrict this list.  See the comments in bits.c for detailed rules and a discussion of the desired coding style.  Note also that casts and arrays are not allowed, that you may only declare variables of type int, and that you may only initialize variables to values in the range 0L - 255L

Grading

Your code will be compiled with gcc and run and tested on one of the CSUG Linux machines.  Your score will be computed out of a maximum of 76 points based on the following distribution:

(41) Correctness
(30) Performance
based on the number of operators used in each function
(5) Style
based on the TAs’ subjective evaluation of the quality of your solutions and your comments

The 15 puzzles you must solve have been given a difficulty rating between 1 and 4, such that their weighted sum totals to 41.  We will evaluate your functions using the test arguments in btest.c.  You will get full credit for a puzzle if it passes all of the tests performed by btest.c, half credit if it fails one test, and no credit otherwise. 

Regarding performance, our main concern at this point in the course is that you get the right answer.  However, we want to instill in you a sense of keeping things as short and simple as you can.  Furthermore, some of the puzzles can be solved by brute force, but we want you to be more clever.  Thus, for each function we’ve established a maximum number of operators that you are allowed to use for each function.  This limit is very generous and is designed only to catch egregiously inefficient solutions.  You will receive two points for each function that satisfies the operator limit. 

Finally, we’ve reserved 5 points for a subjective evaluation of the style of your solutions and your commenting.  Your solutions should be as clean and straightforward as possible.  Your comments should be informative, but they need not be extensive. 

Project Details

Part I: Bit Manipulations

Function Description Rating Max Ops
bitAnd(x,y) (x&y) using only ~ and | 1 8
byteSwap(x,n,m) Swap the nth byte and the mth 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 (and thus 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. 

Part II: 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) xCompute 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.  The “rating” and “max ops” fields have the same meaning as in Part I. 

Part III: 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 (single-precision) floating-point numbers.  The “rating” and “max ops” fields have the same meaning as in Part I. 

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 contants. 

You can use fshow to see what an arbitrary pattern represents as a floating-point number, e.g.

   		./fshow 2080374784 

Advice

You are welcome to do your code development using any system or compiler you choose.  Just make sure that the version you turn in compiles and runs correctly on the CSUG Linux machines.  If it doesn’t compile, we can’t grade it. 

The dlc program, a modified version of an ANSI C compiler, will be used to check your programs for compliance with the coding style rules.  You can also use it to measure the operator counts of your functions.  You can run these tests by executing the command:

       ./dlc -e bits.c 
The program runs only on the CSUG Linux machines. 

Check the file README for documentation on running the btest program.  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, e.g.,

      ./btest -f bitAnd 

The driver.pl is a program that uses both btest and dlc. It takes no arguments.

      ./driver.pl 

“Trivia” assignment

Before noon, Thursday, Jan. 20, send email to with the subject line "[cs252] Trivia 1 - uname" (without the quotes, where uname is your login name) containing answers to the following 4 questions:

  1. Run the following program. 

    #include <stdio.h>
    
    int main(int argc, char *argv[]){
        int bitPattern1 = 0x10011001;
        int bitPattern2 = 0x01100110;
        int bitPattern3 = 0xFFFFFFFF;
        int bitPattern4 = 0x00000000;
    
        /* pair 1 */
        printf("bitPattern1 &  bitPattern2 = %x\n", bitPattern1 &  bitPattern2);
        printf("bitPattern1 && bitPattern2 = %x\n", bitPattern1 && bitPattern2);
    
        /* pair 2 */
        printf("bitPattern1 |  bitPattern2 = %x\n", bitPattern1 |  bitPattern2);
        printf("bitPattern1 || bitPattern2 = %x\n", bitPattern1 || bitPattern2);
    
        /* pair 3 */
        printf("bitPattern1 &  bitPattern3 = %x\n", bitPattern1 &  bitPattern3);
        printf("bitPattern1 && bitPattern3 = %x\n", bitPattern1 && bitPattern3);
    
        /* pair 4 */
        printf("bitPattern1 |  bitPattern3 = %x\n", bitPattern1 |  bitPattern3);
        printf("bitPattern1 || bitPattern3 = %x\n", bitPattern1 || bitPattern3);
    
        /* pair 5 */
        printf("bitPattern2 &  bitPattern4 = %x\n", bitPattern2 &  bitPattern4);
        printf("bitPattern2 && bitPattern4 = %x\n", bitPattern2 && bitPattern4);
    
        /* pair 6 */
        printf("bitPattern2 |  bitPattern4 = %x\n", bitPattern2 |  bitPattern4);
        printf("bitPattern2 || bitPattern4 = %x\n", bitPattern2 || bitPattern4);
    
        return 0;
    } 

    Write the values of the four bitpatterns in binary notation.
    Why do the two outputs differ for pairs 1, 2, 3, 4, and 6? 

  2. What is the output when the following program runs on a CSUG Linux machine?  Why would the output be different if the program were run on a PowerPC-based game console or an old Apple Macintosh? 

    #include <stdio.h>
    int main(int argc, char *argv[]){
        int *translation;
        char bytearray[4];
    
        bytearray[0] = 0x00;
        bytearray[1] = 0x00;
        bytearray[2] = 0xFF;
        bytearray[3] = 0xFF;
    
        translation = (int*) &bytearray;
        printf("Bit pattern 0x00FF = %d.\n", *translation);
        return 0;
    } 

  3. How many lines long is the original bits.c file? 

  4. What does btest print when compiled and run with the original bits.c file? 

What/how to turn in

The “trivia” assignment will be submitted via email.  The main assignment will be submitted using the script /u/cs252/bin/TURNIN.  Watch the Blackboard forum for details, and for any clarifications or revisions to the assignment. 

Before running the TURNIN script, be sure that you have

  • included your full name and email address at the top of bits.c
  • removed any extraneous print statements
  • included any appropriate commentary on your code as C comments in bits.c (there is no separate README file for this assignment)

DUE DATES:

For the “trivia” assignment: noon, Thursday, January 20. 

For the main assignment: 11:59pm, Friday, January 28. 


Last Change: [an error occurred while processing this directive] / Michael Scott's email address