Created
May 29, 2013 10:42
-
-
Save khajavi/5669428 to your computer and use it in GitHub Desktop.
example of conveting float numbers to ieee 754 in single and double precison format.
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> | |
#include <stdint.h> | |
#include <inttypes.h> | |
/* | |
* See also : http://class.ece.iastate.edu/arun/CprE281_F05/ieee754/ie5.html | |
*/ | |
union FloatingPointSinglePrecisionIEEE754 { | |
struct { | |
unsigned int mantissa : 23; | |
unsigned int exponent : 8; | |
unsigned int sign : 1; | |
} raw; | |
float f; | |
} fnumber; | |
union FloatingPointDoublePrecisionIEEE754 { | |
struct { | |
uint64_t mantissa : 52; | |
uint64_t exponent : 11; | |
uint64_t sign : 1; | |
} raw; | |
double d; | |
} dnumber; | |
int main() { | |
printf("sign\texponent\tmantissa\n"); | |
fnumber.f = -6.8; | |
printf("%x\t%x\t\t%x\n", | |
fnumber.raw.sign, | |
fnumber.raw.exponent, | |
fnumber.raw.mantissa); | |
dnumber.d = -6.8; | |
printf("%x\t%x\t\t%" PRIx64 "\n", | |
dnumber.raw.sign, | |
dnumber.raw.exponent, | |
(uint64_t)dnumber.raw.mantissa); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment