How can I get/set individual bits in a float?

floating-point, java

Solution

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.

Problem

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.

Original source