如何打印出一个浮点数的每个位? [英] how to print out each bit of a floating point number?
本文介绍了如何打印出一个浮点数的每个位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想打印出一个浮点数的每一位在C
我可以能够与这个做整数:
INT bit_return(int类型的,诠释LOC)
//位在位置返回
{
INT BUF = A和1 LT;<禄; 如果(BUF == 0)
返回0;
其他
返回1;
}
如果我更换编译器不会编译诠释一个与浮动。
是否有人有办法解决这个?
谢谢了。
的修改的结果
复制和重新格式化低于的结果您COMENT
的希望我没有搞砸的
好吧,人谁是不明确的,我在这里发布我的整个code:
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
INT bit_return(int类型的,整数LOC)//位在位置返回
{
INT BUF = A和1 LT;<禄;
如果(BUF == 0)返回0;
否则返回1;
}诠释的main()
{
int类型的= 289642; //重新$ P二进制$ psent一个
INT I = 0;
对于(I = 31; I> = 0;我 - )
{
的printf(%d个,bit_return(A,I));
}
返回0;
}
解决方案
你要把你的浮到同样大小的一个int的地址,合格的诠释到现有功能的地址。
I am trying to print out each bit of a floating point number in c.
I can be able to do it for integer with this:
int bit_return(int a, int loc)
// bit returned at location
{
int buf = a & 1<<loc;
if (buf == 0)
return 0;
else
return 1;
}
the compiler wouldn't compile if I replace int a with float a.
Does anybody have a solution for this?
Thanks much.
Edit
Copy and reformat your coment below
Hope I didn't mess it up
Ok, for people who are not clear, i post my whole code here:
#include <stdio.h>
#include <stdlib.h>
int bit_return(int a, int loc) // bit returned at location
{
int buf = a & 1<<loc;
if (buf == 0) return 0;
else return 1;
}
int main()
{
int a = 289642; // represent a in binary
int i = 0;
for (i = 31; i>=0; i--)
{
printf("%d",bit_return(a,i));
}
return 0;
}
解决方案
Cast the address of your float to the address of an int of the same size, and pass that int to your existing function.
这篇关于如何打印出一个浮点数的每个位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文