浮点数的内存存储方式
2008-05-18 16:54
323 查看
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#define GET(p) (*(p) | (*(p+1) << 8) | (*(p+2) << 16) | (*(p+3) << 24))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void printb(unsigned int n)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
unsigned char B[32] = ...{0,};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bool flag = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int j = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (int i=31;i>=0;i--)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
unsigned int p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p = n << 31 - i >> 31;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (!flag)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (p == 1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
flag = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (flag)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
B[j] = (p == 1)?'1':'0';
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
j ++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("%s",B);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int main(int argc, char* argv[])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
float f = -289.95;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//单精度浮点
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
unsigned int sign;//符号
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
unsigned int significand;//有效位
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
unsigned int exponent;//指数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
unsigned int floatbyte;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
unsigned char * pfloat = (unsigned char*)&f;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
floatbyte = GET(pfloat);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
sign = floatbyte >> 31;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
exponent = floatbyte << 1 >> 24;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
significand = floatbyte << 9 >> 9;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("符号位:");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printb(sign);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf(" 指数位:");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printb(exponent);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf(" 有效位:");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printb(significand);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//输出结果
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf(" 单进度浮点数为:");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (sign == 1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("-");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} else...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("+");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("1.");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printb(significand);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//这里只讨论指数为规范化值的情况,对于零和无穷大不讨论
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (int(exponent - 127) > 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf(" >> %d ",exponent - 127);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} else ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf(" << %d ",127 - exponent);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
以上代码只为了演示单精度浮点数在内存中的存储方式,双精度也类似。想了解更多,请参照IEEE Standard 754 Floating-Point标准。
相关文章推荐
- 移码及浮点数在内存中的存储方式
- C语言中浮点数在内存中的存储方式
- 浮点数在内存中的存储方式
- 浅谈C/C++的浮点数在内存中的存储方式
- C语言中浮点数在内存中的存储方式
- 算法竞赛入门经典------浮点数整数在内存中的存储方式
- C/C++的浮点数在内存中的存储方式分析及实例
- 浅谈C/C++的浮点数在内存中的存储方式
- 浅谈C/C++的浮点数在内存中的存储方式
- 浮点数在内存中的存储方式
- 浮点数在内存中的存储方式
- 深入理解计算机系统-之-数值存储(五)--浮点数在内存中的存储方式
- 浅谈C/C++的浮点数在内存中的存储方式
- 大小端以及浮点数在内存中的存储方式
- [算法]浮点数在内存中的存储方式
- 浅谈C/C++的浮点数在内存中的存储方式
- 浅谈C/C++的浮点数在内存中的存储方式
- 浅谈C/C++的浮点数在内存中的存储方式
- C语言中浮点数在内存中的存储方式
- 浅谈C/C++的浮点数在内存中的存储方式