cs270 Programming Assignment PAx - Bit Fields in C


Due: xx/xx/xxxx @ 11:59PM
Key: Use the key FIELD for checkin

About The Assignment

This assignment is designed to teach you how to manipulate the bits of integer values. It 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 5 functions) that enables getting and setting subfields in a bitvector. This is especially useful for applications like network packet processing. We will use it later in this class for understanding number representations and for converting LC3 assembly code into machine code. First 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.

This article may give you hints on how to do various bit operations.

Jack Applin found that the expression v - ((v<<1) & (1<<w)) sign-extends the value in v which is w bits wide.

NOTE: No loops are required in this assignments. Use of loops will be penalized.

Getting Started

Perform the following steps
  1. 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. PAx).
  2. Copy the three files into this directory. It is easiest to right click on the link, and do a Save Target As.. for each of the files.
  3. Open a terminal and make sure you are in the directory you created in step 1. The cd command can be used for this.
  4. In the terminal type the following three commands to build the executable.
        gcc -g -Wall -c -std=c99 testField.c
        gcc -g -Wall -c -std=c99 field.c
        gcc -g testField.o field.o -o testField
  5. In the terminal type testField and read how to run the the program.
  6. 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.

Computing Bit Masks

In this assignment you will need to compute masks whose values (in binary) have a variable number of consecutive 1's. To help you understand how to do this, the followings exercise may help. This is a pencil and paper exercise. You are going to create a table with multiple rows and columns.
  1. The 1st column will be labeled N and will contain the numbers 0, 1, 2, 3, 4, 5, ... This range is probably sufficient, but you may want to go higher.
  2. The 2nd column will be a string of charaters starting with a 1 and followed by exactly N 0's.
  3. The 3rd column will be the number obtained by converting the binary digits in column 2 to a number.
  4. The 4th column will be a string of characters beginning with a zero and followed by exactly N 1's.
  5. The 5th column will be the number obtained by converting the binary digits in column 4 to a number.
Now find a relationship between columns 3 and 5. Now consider the new C operators you are learning about and see if you can find a simple way to compute column 2 using the number 1, an operator and N. Once you understand this, the remainder of the assignment is much simpler.

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.

  1. Write some/all of one function in field.c using your favorite editor.
  2. Save your changes and recompile field.c using gcc -g -Wall -c -std=c99 field.c. You will find it convenient to work with both a terminal and editor window at the same time.
  3. Repeat steps 1 and 2 until there are no errors or warnings.
  4. Build the executable using gcc -g testField.o field.o -o testField.
  5. Test the function you have been working on. Do not attempt to move on until you complete and thoroughly test a function.
  6. Repeat steps 1 thru 5 for the remaining functions.

Checking in Your Code

You will submit the single file field.c using the checkin program. Use the name FIELD. At the terminal type:

    ~cs270/bin/checkin FIELD field.c

The above command submits your assignment. For a sanity check, type the following to get the file you checked in and make sure it compiles and runs properly with the provided files:

    mkdir sanityCheck
    cd sanityCheck
    ~cs270/bin/peek FIELD field.c > field.c
    cp ../field.h ../testField.c .
    gcc -g -Wall -c testField.c
    gcc -g -Wall -c field.c
    gcc -g testField.o field.o -o testField 
    // Do LOTS of test cases.
Relax, you are done with your assignment!