CompOrg Fall 2002 Homework #2

C program to display C ints & floats in binary or hex

Due Date: 9/26/02 (by 11:59:59PM)
Only Electronic Submissions!

Late Penalty: 10% per day

Assignment

This assignment involves the creation of a C program which can be compiled and run under Unix (any of Linux, FreeBSD, Cygwin are fine, we will test with the compiler gcc).

You are to write a program that is capable of printing out either the hex or binary representation of various numeric values. Command line parameters will be used to tell your program what representation should be used (hex or binary), and what value to display. Your submission must include a single C file named "hw2.c", and it must be written to work with the command line parameters described below. We will use an automated system to run your program, so if you don't follow the instructions below we won't be able to grade your program.

We will compile your program with the command line "gcc -o hw2 hw2.c" (the resulting executable program will be named hw2). The command line parameters are listed below:

  1. The first command line parameter will be a single character that indicates what representation should be used to display the value. The only possible characters used will be 'b' (for binary) and 'x' (for hex).

  2. The second command line parameter will indicate whether the the value is an integer ('i') or a floating point number ('f').

  3. The third command line parameter is the actual value (specified as a decimal number) that we want you to print out. Integer values should be treated as signed int (32 bit 2s complement integers). Floating point values should be treated as the C type float, which is IEEE single precision (32 bits long).

For example, the command line ./hw2 x i 13 means "print the hex representation of the int with the value 13".

The output format for the hex representation of either an integer or a float (which is single precision IEEE) should simply be an 8 character hex string followed by a newline (\n). Use capital letters for the hex digits (use 'A', 'B', not 'a', 'b'). The %X format for printf will do all the work!

The output format for the binary representation of integers should be a string of 32 binary digits (bits). You need to actually print a '1' or a '0' for each bit. There is no binary format for printf, so you need to write code to print a sequence of '1's or '0's to match the bits in value you are given.

For floating point numbers, you need to print 32 bits, but you should put a single space after the 1st bit (which represents the sign bit in IEEE format), then print the 8 bit exponent followed by a space, then the 23 bits of the significand. So the output of the floating point value 7 would be: 0 10000001 11000000000000000000000

Examples

A few examples of the output expected for various command line parameter values are shown below. '>' is the shell prompt.

Printing the integer 38 in hex
> ./hw2 x i 38
00000026
  

Printing the integer 38 in binary
> ./hw2 b i 38
00000000000000000000000000100110
  

Printing the integer -1 in hex, then binary
> ./hw2 x i -1
FFFFFFFF
> ./hw2 b i -1
11111111111111111111111111111111
>
  

Printing the float 7 in hex, then binary
> ./hw2 x f 7
40E00000
> ./hw2 b f 7
0 10000001 11000000000000000000000
  

Printing the float -12.125 in binary
> ./hw2 b f -12.125
1 10000010 10000100000000000000000
  

How to submit

Submission of your homework is via email, the general idea is to send an email message with your file as an attachment. There is an automated email submission system that will respond to your submission right away, so you will have a record that we got your file.

All projects must be submitted via email to comporg-submit@cs.rpi.edu. The subject line of the submission message should contain a single number '2' indicating the HW number. You must include your C program named hw2.c as an attachment.

IMPORTANT! Make sure your email message includes your full name, we can't record your grade unless we know your name!

Don't send compiled code!

You can expect a return email indicating receipt of your project submission immediately. This receipt will include a list of all the files that were successfully extracted by the submission script - please look over the receipt carefully to make sure your submission worked.

Multiple Submissions: You can resubmit up to 10 times for each project, we will always grade the last submission received unless you tell us otherwise.

Grading

Grades will be determined by testing your program with various valid command lines (we will not test with invalid options). Each combination of data type and output representation format will be tested with a few values, each combination counts for 25% of the grade.

HINTS: