ICS 331 Assignment #1

In C, write a program to read an int from stdin:


int myint;
...
scanf("%d", &myint);

Then print out the int to stdout as a binary bit string (1's and 0's) with one space between bits and an extra space every four bits (printing the LSB last). Make your program independent of the local size of an int by using sizeof(int) to determine the number of bytes used by an int. Submit a sample run on both your own machine and on uhunix2. Your program should be named binprint with your source code files named binprint.c and binprint.h (if necessary) and your sample outputs labeled binprint_pc.txt and binprint_unix.txt (or .pdf or .jpg or some other image extension depending on the exact format of your sample output screendump).

In C, write a program that determines whether the computer on which the program is running is big-endian or little-endian. Submit a sample run on both your own machine and on uhunix2. Report the exact processor type of your own machine (e.g., Intel Pentium 4 570J 3.8GHz overclocked to 4GHz or AMD Athlon 64 4000+). Your program should be named endian with your source code files named endian.c and endian.h (if necessary) and your report named endian.txt or .doc or .pdf.

In C, write a program to compare the time to execute integer addition versus integer multiplication versus integer division. You will need a large loop (I needed 100,000 iterations) to be able to detect any differences. Repeat for short and long int's. Repeat for floats, doubles, and long doubles using the same number of iterations. Summarize your results in a spreadsheet with columns add, mul, div and with rows int, short, long, float, double, long double. Run this program twice, once on your own machine and once on uhunix2 (so you should have two spreadsheet tables, one for your own machine and one for uhunix2). Report the exact processor type of your own machine (e.g., Intel Pentium 4 570J 3.8GHz overclocked to 4GHz or AMD Athlon 64 4000+). Your program should be named arithtime with your source code files named arithtime.c and arithtime.h (if needed) and your spreadsheet named arithtime.xls or .csv or .doc or .pdf.

Be sure to include a makefile that will run on uhunix2 and builds all three of your programs. To submit your assignment, zip all of your files (your .c and any .h files, your makefile, your sample output files, your report, and your spreadsheet) and email to our TA, dwkang@hawaii.edu.

Here are some links to C code on the web related to these exercises. DO NOT COPY AND PASTE their code into your programs! You are welcome to read this code and use their ideas in your code. However, write your own code and do not look at their code while writing your own. In other words, read their code and spend long enough perusing it so that you are sure you know what to do, then put their code away and write your own version without looking back at their code.

Martin Leslie has a C program for printing out a byte in binary format. Unix Incompatibility Notes: Byte Order has some simple ideas with code fragments for testing little vs. big endian. Intel XScale Microarchitecture Programmer Model for Big Endian has somewhat more complex but complete code for big vs. little endian testing.

The GNU C Library has functions for both Processor And CPU Time measurement . It is important to use these types of time measurements, since on a time-shared system like uhunix2, if you measure elapsed real time, you will be measuring other people's program time as well as your own. Even on your own machine, you will get mixed into your elapsed time the time taken by other processes (this is why Windows and UNIX, including Mac OS X, are not considered good real-time operating systems and why there is a small market for real-time OSes).


David N. Chin
Last modified: Mon Sep 9 15:06:20 HST 2002