• 0

[C] IEEE-754


Question

I'm supposed to make a program that converts an input number into IEEE-754 format (in hex)

I'm not really familiar with C at the moment, so I'm stuck...

I think we're supposed to save the input to memory, where it'll be saved in IEEE-754 format and have the program read it back in that format and output it...

can anyone give me any hints?

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

IEEE754 with how many bits? Also, is the output supposed to be the bitstream used to represent it? I'm pretty sure float in C is IEEE754 32 bit and double is IEEE754 64 bit.

Link to comment
Share on other sites

  • 0

it asks for IEEE-754 with 32bits, I'm not really sure what you mean by bitstream used to represent the output, it just says to output the internal machine representation as an 8 digit hex number

the example given in that link seems to be a bit complicated

we were told that the code shouldn't be more than a few lines, and i think should be only one method

a hint we were given was to use a union, but im not sure what that does...

Link to comment
Share on other sites

  • 0
it asks for IEEE-754 with 32bits, I'm not really sure what you mean by bitstream used to represent the output, it just says to output the internal machine representation as an 8 digit hex number

the example given in that link seems to be a bit complicated

we were told that the code shouldn't be more than a few lines, and i think should be only one method

a hint we were given was to use a union, but im not sure what that does...

I think the code is reasonably complex due to the fact that its converting the float number using the exact IEEE754 standard, so that it's accurate to god knows how many bits. If you're just looking for a generic float to integer, there should be plenty of code online.

I'm doubtful you can do in a few lines of code using that standard. The reason for the two methods is that there's one to convert from a float to an 8bit hex number, and the other to convert back to float.

Also regarding unions, they're basically the same as structures, but the same memory locations can be accessed by different data types. There's more info here: http://www.cplusplus.com/doc/tutorial/other_data_types/

Link to comment
Share on other sites

  • 0

Wow, I didn't know about unions, nice to learn something.

Basically what you have to do is set a union with a float and (i think) an unsigned byte, then set the value you want for the char and the start using printf %x option to print the char in hex, you will need to increase the char pointer 3 times since there are 8 bits in a char and 32 in a float.

Link to comment
Share on other sites

  • 0

Haha looks like I learned something too Argote. Your post gave me a sort of epiphany. Unions aren't something I'd typically use but I gave them a go! :p

Here's code that'll do what you want and it uses a union. I'm still pretty sure its not the IEEE754 standard though.

#include <stdio.h>
#include <stdlib.h>

union mytypes_t {
	unsigned short i[1];
	float f;
} myTypes;


int main()
{
	printf("Enter a float: ");
	scanf("%f", &myTypes.f);

	printf("upper 16 bits - %04X\n", myTypes.i[1]);
	printf("lower 16 bits - %04X\n", myTypes.i[0]);

	return 0;
}

An unsigned short is 16 bits long, so you need two of them, hence the 2 element array. You could also use Argote's method of using a char, but as a char is 8 bits long, you would need char c[3].

Hope this helps!

Edited by ViZioN
Link to comment
Share on other sites

  • 0

thanks for the help guys

it turns out I was already given the answer during the tutorials

we were told to create a union with a float and a double, use scanf and save it to the address of the float

and then use printf and return the double

im still not quite grasping what's going on though... not understand C in general (I only know some java)

for my next question, i'm completely stumped...

we're supposed to create a function in C that performs signed binary division

i understand how binary division works, i can do it on paper pretty easily, but i'm not sure how to begin to implement it in code...

Link to comment
Share on other sites

  • 0

The union allows the same memory address for different data types. Using the code example above, when the user enters a float it is stored in the myTypes union which is 4 bytes wide.

If we type myTypes.f we interpret the memory address as a float. Or in the case above as two unsigned shorts which are 16 bits each, giving a total of 4 bytes.

All you're doing is altering how you interpret the same memory.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.