tags:

views:

792

answers:

5

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 that 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.

+6  A: 

http://java.sun.com/developer/Books/shiftintojava/page1.html

The designers of the Java programming language chose to omit the union construct because there is a much better mechanism for defining a single data type capable of representing objects of various types: subtyping. A discriminated union is really just a pallid imitation of a class hierarchy.

Includes examples.

skaffman
Yes, but a class hierarchy is no good here. I want to be able to set it as one type and read it as another. A class hierarchy will force me to set it and read it as the same type.
paxdiablo
+1 for very nice link
dfa
Let's back up - is there a way to change one bit in a float? That's what I'm looking for. I want to be able to set (for example) bits 31, 27, 4, and 0 of the float to see what value that is.
paxdiablo
int bits = Float.floatToIntBits(aFloat); /* use bitwise operators in the IEEE 754 bit vector */ aFloat = Float.intBitsToFloat(bits);
dfa
Is the question about float manipulation or unions? The two seems to have nothing to do with each other.
skaffman
+1 for finding that. It's bookmarked for future ref.
paxdiablo
@skaffman, union would be how I would have done it with C. I didn't realize that the Float class provided raw bit conversions, - the second sentence in the question is probably the one that's more relevant.
paxdiablo
Yeah, I remember unions now... it's been a while since I did any C :)
skaffman
+3  A: 

Some useful articles & faq to this subject are,

Union types in Java?

Substitutes for Missing C Constructs

adatapost
+1 for the info.
paxdiablo
+7  A: 

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.

Michael Borgwardt
Bang. That's what I was after. And I see the reverse is Float.floatToRawIntBits. Now I just need the Double/Long variant but I assume, if there is one, it'll be in the Double class. I'll go find that. Thanks heaps.
paxdiablo
I'm still confused... what does this have to do with unions?
skaffman
And +1 .
paxdiablo
The connection with unions is that they allow multiple datatypes to exists in the same memory location, providing the ability to effectively convert from one type to another. Pax needed to do a similar thing with an int and a float and, coming from a C background, naturally fell to the union approach - but couldn't find how to do this in Java. Luckily for him, his particular scenario is dealt with by the standard libraries.
belugabob
Yeah, my only other thought was to write the int to a file and read it back as a float. I was pretty certain there was a better way than *that*. Thanks, guys, SO proves its worth yet again.
paxdiablo
A lovely example for how easy it is to fall into the trap of assuming that in order to achieve X you have to do Y, then ask how to do Y rather than how to achieve X.
Michael Borgwardt
Ah, gotcha. Yes, that was a rather roundabout question to achieve something simple :)
skaffman
+2  A: 

Java doesn't provide any way to treat a value as another value on raw bit level - no unions, nothing like reinterpret_cast. However, for your particular case of treating float as int and vice versa, you can use java.lang.Float.floatToIntBits and intBitsToFloat methods.

Pavel Minaev
+1 for the info, even though @MB just beat you to it. Thanks.
paxdiablo
+2  A: 

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

Union'ing between pointer types (or between pointers and their numeric represetantion) would open holes in the type system, so it is strictly impossible.

mfx
+1. and this finishes off my quest (I'll need this for double/long later on). Ta.
paxdiablo