十进制小数到二进制数的转换 [英] Decimal Fraction to Binary Number Convertion
本文介绍了十进制小数到二进制数的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一个将我实际拥有的小数部分转换为二进制变量的函数.
谁能帮我这个忙...
Thnx
Hi,
I''m looking for a function which convert the decimal fraction i have in real, to a binary var.
Can anyone please help me on this one...
Thnx
推荐答案
这是我之前编写的函数.当它存储在内存中时,它以1英寸和0英寸打印一个float
变量(它以IEEE单精度浮点数标准存储).
Here is a function that I have written before. It prints afloat
variable in 1''s and 0''s as it is stored in the memory (it is stored with the IEEE single precision floating point numbers standard).
void printFloat(float a)
{
unsigned long* temp = (unsigned long*)&a;
unsigned long one = 1;
for(int i = 31; i >= 0; i--)
{
if(((*temp) & (one << i)) == 0)
cout << "0";
else cout << "1";
if (i % 4 == 0)
cout << " ";
}
cout << endl;
}
我不确定这是否是您所需要的.希望对您有所帮助.
I''m not sure if this is what you need. Hope it helps.
这篇关于十进制小数到二进制数的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文