我怎样才能获得/设置个人位在浮动? [英] How can I get/set individual bits in a float?
问题描述
我有一个整数,我正在设置个别位,我想打印出了等效的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屋!