我怎样才能获得/设置个人位在浮动? [英] How can I get/set individual bits in a float?

查看:185
本文介绍了我怎样才能获得/设置个人位在浮动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须承认,在我所有的Java工作中,我从来没有遇到过需要Java联盟(比如C联盟,而不是SQL联盟),我在这里找不到答案。无可否认,我在Java中的大多数工作都比抽象的抽象更高。



我有一个整数,我正在设置个别位,我想打印出了等效的IEEE754单精度浮点数。



在C中,我会做类似于:

  union {
int i;
float f;
} x;
x.i = 0x27;
printf(%f \ n,x.f);

如何在Java中做类似的事情?是否有可能将相同的内存作为Java中的两种不同的数据类型对待?



我在SO和其他地方搜索了java union,但是它让我茫然SQL的东西 - 我找不到这样做的方法。

解决方案


我有一个整数我设置了
个别位,我希望
打印出等效的IEEE754
单精度浮点数。


使用 intBitsToFloat()


I have to admit in all my work with Java, I've never come across the need for a Java union (like the C union, not the SQL one) and I cannot find an answer here on SO. Admittedly, most of my work in Java has been at higher abstractions than bit-fiddling.

I have a integer which I'm setting individual bits for and I want to print out the equivalent IEEE754 single-precision float.

In C, I'd do something like:

union {
    int i;
    float f;
} x;
x.i = 0x27;
printf ("%f\n", x.f);

How do I do a similar thing in Java? Is it even possible to treat the same memory as two different data types in Java?

I did search on both SO and elsewhere for "java union" but it swamped me with SQL stuff - I couldn't find a way to do this.

解决方案

I have a integer which I'm setting individual bits for and I want to print out the equivalent IEEE754 single-precision float.

Use intBitsToFloat() for that.

这篇关于我怎样才能获得/设置个人位在浮动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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