Computer Science 220
Assembly Language & Computer Architecture

Fall 2010, Siena College

Lab 1: C and Bitwise Operations
Due: 10:00 AM, Friday, September 24, 2010

For this lab assignment, you will learn how to write and run a simple C program, and then write a few more programs to try out some of its bitwise operators.

You may work alone or with a partner on this lab.

The first few sections of this lab handout contains some examples that you should work through during lab. Once you have completed those, you can move on to the C programming assignments. However, even if you have not gone through the examples (maybe you're in the Friday lab), you can start thinking about how to solve these problems. Maybe even solve them using your favorite programming language and convert them to C later.

A Very Simple C Program

We will begin by seeing how to compile and run a very simple C program (hello.c) in a Unix environment.

See Example:

We will assume that we are working at the Unix command line. If you have not already done so, you will need to boot into Linux and open a Terminal window.

For you to run this, you will want to copy the example to your own directory. But first, we'll create a directory for it:

mkdir ~/hello

This creates a new directory (folder) in your home directory (indicated by the ~) called hello.

To copy the example from the shared area to your new directory:

cp ~jteresco/shared/cs220/examples/hello/hello.c ~/hello

We now change directory to the copy in your own directory:

cd ~/hello

And compile and run it:

gcc hello.c

Things to note from this simple example:

A Bit More Complex Example

We next consider an unnecessarily complicated C program that computes the greatest common denominator of two integer values.

See Example:

Lots of things to notice here:

The bad news: that was a lot of trouble just to write a simple program.

The good news: you will have examples to look at and you can ask a lot of questions.

C Programming Assignments

The main part of the lab is to write two C programs. The first should be fairly straightforward, but the second will require some more careful thought. For both, you should try to come up with an elegant and simple solution. Avoid expensive operations like multiplication, division, and nested loops. Keep in mind that bitwise operations are the focus of this assignment.

If you're having trouble, ask questions! Make sure that your code is well written and documented. I encourage, but will not require, that you develop them using Unix. You are, however, required to make sure your programs run properly on the Linux systems in Roger Bacon 306 before you submit them.

  1. Write a C program counttheones.c that takes a signed int value and prints out the number of bits in the number's internal representation that have the value 1. Your program may prompt for the input value of accept it on the command line. You may assume the value on the input is an integer.
  2. A certain security system includes a keypad where access codes are entered as 5 digit numbers. On this particular system, 1 and 2 are on the same button, as are 3 and 4, etc. 9 and 0 are on the same button as well.

    Write a C program access.c that reads in a five digit access code as a decimal number and writes out each of the five digit access codes that are equivalent (there are 32). Make sure that you print any leading zeros. Again, your program may prompt for the input value or accept it on the command line. You may assume that the value on the input is an integer.

Submission and Evaluation

This lab is graded out of 25 points.

By 10:00 AM, Friday, September 24, 2010, submit documented source code for your two programs. All necessary files should be submitted by email to jteresco AT

Grading Breakdown

counttheones.c correctness 5 points
access.c correctness 8 points
Efficiency, style and elegance 7 points
Documentation 5 points