| CompOrg Fall 2002 Homework #2 FAQ | ||
| CompOrg Home | Syllabus | HW2 Description | ||
| 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 |
|   |   |
| Question: |
I compile my code with gcc, but the output file will not
execute. I use the command |
| 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 #include <stdlib.h>
You can tell the gcc compiler to complain about such things (and many
more), by including the 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 |
|   |   |
| 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: |
In C, to compare strings you use
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')
|
|   |   |