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

I have to admit to all my work with Java, I have never encountered the need to combine Java (for example, C-connections, not SQL-code), and I can not find the answer here on SO. Admittedly, most of my work in Java was on higher abstractions than bit-fiddling.

I have an integer for which I set the individual bits, and I want to print an equivalent float with one precision IEEE754.

In C, I would do something like:

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

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

I searched both SO and elsewhere for "java union", but it covered me with SQL materials - I could not find a way to do this.

+5
5

, IEEE754 float .

intBitsToFloat() .

+12

C

Java- , , : . - pallid .

.

+9

Java - , reinterpret_cast. float int , java.lang.Float.floatToIntBits intBitsToFloat.

+5
source

If you are interested in accessing the float representation at the bit level, java.lang.Double contains a method for this (doubleToLongBits, etc.).

The connection between the types of pointers (or between pointers and their numerical representation) will open holes in the type system, so this is not possible.

+4
source

Some useful articles and answers to this question,

Connection types in Java?

Replacements for missing C designs

+3
source

All Articles