Created
May 28, 2012 08:54
-
-
Save bungoume/2818041 to your computer and use it in GitHub Desktop.
print ieee754(double) data
This file contains 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 <tchar.h> | |
#include <iostream> | |
typedef struct { | |
unsigned int fraction1: 32; | |
unsigned int fraction2: 20; | |
unsigned int exponent: 11; | |
unsigned int sign: 1; | |
}field; | |
typedef union { | |
double ex; | |
field bit; | |
}myDouble; | |
void print2bin(unsigned long long val,int len){ | |
char ans[65]; | |
for(int i = len-1; i>=0; i--){ | |
ans[len-1-i] = ((val >> i) & 1) ? '1':'0'; | |
} | |
ans[len]='\0'; | |
std::cout << ans << std::endl; | |
} | |
int _tmain(int argc, _TCHAR* argv[]) | |
{ | |
myDouble data; | |
unsigned long long fraction; | |
int exponent; | |
for(;;){ | |
std::cout << ">>"; | |
std::cin >> data.ex ; | |
exponent = (data.bit.exponent)-1023; | |
fraction = (unsigned long long)1 << 52; | |
fraction += (unsigned long long)data.bit.fraction2 << 32; | |
fraction += (unsigned long long)data.bit.fraction1; | |
std::cout << "sign =" << data.bit.sign << std::endl; | |
std::cout << "exponent =" << exponent << std::endl; | |
std::cout << "fraction =" << std::hex << std::showbase << fraction << std::endl; | |
std::cout << "fraction =" ; print2bin(fraction,53); | |
std::cout << "data =" << std::dec<< (fraction >> (52-exponent)) << std::endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment