如何显示浮点数或双精度浮点数的二进制表示形式? [英] How do I display the binary representation of a float or double?

查看:1151
本文介绍了如何显示浮点数或双精度浮点数的二进制表示形式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一个浮点数的二进制(或十六进制)表示。我知道如何手动转换(使用此处)的方法,但虽然我对C ++和Java解决方案特别感兴趣,但是我想知道是否有任何语言使得它非常容易所以我正在使这个语言不可知。我很想看到其他语言的一些解决方案。



编辑:我已经得到了C,C ++,C# Java的。是否有任何替代语言大师在那里谁想要添加到列表?

解决方案

C / C ++很容易。 p>

  union ufloat {
float f;
unsigned u;
};

ufloat u1;
u1.f = 0.3f;

然后输出 u1.u 。您可以修改此实施



<双倍合并。

 联合udouble {
double d;
unsigned long u;因为双打是64位。





$ b

Java比较容易:使用 Float.floatToRawIntBits() Integer.toBinaryString() Double.doubleToRawLongBits Long.toBinaryString()


I'd like to display the binary (or hexadecimal) representation of a floating point number. I know how to convert by hand (using the method here), but I'm interested in seeing code samples that do the same.

Although I'm particularly interested in the C++ and Java solutions, I wonder if any languages make it particularly easy so I'm making this language agnostic. I'd love to see some solutions in other languages.

EDIT: I've gotten good coverage of C, C++, C#, and Java. Are there any alternative-language gurus out there who want to add to the list?

解决方案

C/C++ is easy.

union ufloat {
  float f;
  unsigned u;
};

ufloat u1;
u1.f = 0.3f;

Then you just output u1.u. You can adapt this implementation.

Doubles just as easy.

union udouble {
  double d;
  unsigned long u;
}

because doubles are 64 bit.

Java is a bit easier: use Float.floatToRawIntBits() combined with Integer.toBinaryString() and Double.doubleToRawLongBits combined with Long.toBinaryString().

这篇关于如何显示浮点数或双精度浮点数的二进制表示形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆