Lab 4 - Bit layout

You will write 3 small C programs using cygwin/gcc as your development environment. You may work together with a partner (or two) if you want, but make sure that each of you fully understands what is happening in the lab. THIS LAB SHOULD BE SUBMITTED.

Submit the final version of the C code (the .c file) and a simple text write up of the questions below. The typical answer (per question) should be a few sentences. You may include the writeup as comments in your source code.

To submit the lab, send an e-mail with the header "lab4 submission" to me at cdamon@vtc.edu. Be sure to attach your source code.

Some guidelines for using unix command line (like cygwin) can be found here. There is also a cheat sheet for compiling with gcc here.

1. Which end?

Write a simple C program that can determine whether a machine is little- or big-endian.

2. Printing binary

Write a function void printBin(int value) that will print an integer as a binary number.

Hint: You want to print a 1 or a 0 based on the high order bit, then you need to move the next to high order bit into the high order bit. You will need to explicitly count the number of bits you have printed.

Write a main method that calls printBin multiple times with a few different values to test it. You should try at least a positive number, a negative number and 0.

Your submission should answer the following questions about this program:

3. Floating point struct

Define a struct Single that describes the bit layout for a single precision floating point number.

You will need to use bit fields in the struct. Notice that you need to consider little endian vs. big endian.

Your submission should answer the following questions about this program:

4. Printing floating point parts

Define a method void printSingle(float f) that will print the parts of the floating point number as distinct values.

A typical output should look like:

For value 3.5, sign=0 exp =128, fraction=11000000000000000000000

To place the float into the struct, use a pointer cast or a union. For example, to interpret the bits of a floating point number as an integer, you could say

int i;
*(float *)&i = f;

To print a floating point number in printf, you typically use %f.

Your submission should answer the following questions about this program: