Created
May 29, 2013 04:25
-
-
Save khajavi/5667960 to your computer and use it in GitHub Desktop.
Converting floating-point number to IEEE754 representation by using union and struct in c.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
/* | |
* See also : http://class.ece.iastate.edu/arun/CprE281_F05/ieee754/ie5.html | |
*/ | |
union FloatingPointIEEE754 { | |
struct { | |
unsigned int mantissa : 23; | |
unsigned int exponent : 8; | |
unsigned int sign : 1; | |
} raw; | |
float f; | |
} number; | |
int main() { | |
number.f = -6.8; | |
printf("%x, %x, %x", number.raw.mantissa, number.raw.exponent, number.raw.sign ); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not pretty, but I came up with this one to properly display the number. @kamalhyder