CompOrg Fall 2002 Homework #2 FAQ


Homework #2 FAQ

Question:

The compiler complains when I try to include code to shift a float (or use bitwise logic operations). How can I extract the bits from a float variable?

Answer:

You have a 4 byte value in memory that holds the IEEE representation of some value. If you tell the compiler to treat those 4 bytes as an int, you can use shifts and logic operations. Something like this:

  float f;
  int *ipointer;

  ipointer = (int *) &f;    // ipointer points to the 4-byte IEEE representation

  // now you can use *ipointer anywhere you can use an int.

   
Question:

My program is getting quite large, am I doing something wrong?

Answer:

People who insist on use arithmetic instead of logic operations tend to have much larger programs. This entire project is very doable is less than 100 lines of commented C code.

If you find yourself using the pow function, or you are attempting to calculate the actual bits that make up the IEEE representation - you are doing too much! If you care about whether a number is normalized or not - you are doing too much! If you have a float variable with the right value, you already have the IEEE representation, just look at the bits to see what it is (there is no reason to try to calculate them).

   
Question:

I compile my code with gcc, but the output file will not execute. I use the command gcc -o hw2 hw2.c but then when i type in hw2 on the command line it says it dosen't exist (but ls says it does and it's executable).

Answer:

You do not have "." in your path. Just type "./hw2", this tells the shell where to look for your program (in the current directory).

   
Question: The function atof() doesn't seem to be working for me, why not?
Answer:

The prototypes for atoi and atof are defined in stdlib.h so you need to add this to the top of your program:

#include <stdlib.h>

You can tell the gcc compiler to complain about such things (and many more), by including the -Wall switch to the compiler (a command line option). This tells the compiler to print all warning messages. So now your command line would look like this:

gcc -Wall -o hw2 hw2.c
   
Question:

Do I have to print leading zeros ?

Answer:

YES! As stated in the assignment, your program should print exactly 8 hex digits (or 32 binary digits).

For example, the command line ./hw2 x i 0 should generate the output 00000000, not 0.

   
Question:

Why doesn't this work (the comparisons don't seem to work, although they compile):

if(argv[2] == "f") { /* float type */

    valueF = atof(argv[3]);

} else if(argv[2] == "i") { /* integer type */

    valueI = atoi(argv[3]);

}
Answer:

argv[2] is of type char* (a pointer to a char). You are attempting to compare a pointer to another pointer (the literal "f" is also a pointer to a char). Remember that the value of a pointer is just the address it holds, not what is at that address.

In C, to compare strings you use strcmp():

if (strcmp(argv[2],"f")==0) {
    // whatever...
}

In this case, you are looking for a string with exactly one character, so you might also use this:

if (*argv[2] == 'f')

*argv[2] is the char pointed to by argv[2], and notice that it is compared to a char 'f', not a string "f". In C, double quotes mean a string, single quotes are for a single char.