File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Conversion from Floating Point to Binary (not using Wrapper Class Methods) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Conversion from Floating Point to Binary (not using Wrapper Class Methods)" Watch "Conversion from Floating Point to Binary (not using Wrapper Class Methods)" New topic

Conversion from Floating Point to Binary (not using Wrapper Class Methods)

Robert Kausch

Joined: Oct 31, 2006
Posts: 3
Hi there, long time reader, first time participant. I have run into a rather benign (or, as I thought at first, benign) problem. I'd love to convert from a double or float to the bitwise representation for some underlying storage. Well, I know there's the Float.floatToIntBits and the corresponding methods to back from int bits to a float.

Problem is, I really don't want to use the wrapper class (namely because I need to port the code to c# when I'm done, and I'd like to reuse as much code for that half of the project as possible). Also, I'm just really interested in how it works.

So, logically, here's how I would do it, if the operators would let me:

Anyway, I'm getting really frustrated that I can't seem to find anything that points me in the right direction, and I'm beginning to believe my Google skills are starting to falter I looked into the Java source's and, but the two methods rely on native code, which I also cannot seem to find.

Any tips?

Thanks a million!
Your curious code monkey,
The Dude.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199


Welcome to JavaRanch!

We have a strict policy on display names, which must be a real first and last name with a space between.

Please go here and fix your display name up, pronto. Thanks, pardner!

Now, as to your question: can't be done (in any sensible way). As you've proven to yourself after a long search, the only way to get access to the bits behind a float or double are via the native methods you're already aware of. The Java language itself provides no way to do it.

If you're far more desperate than you should be to stick to your guns on this one, the only thing I can think of would be to code a long, elaborate process in which you determine the mantissa and exponent by dividing by various powers of ten and looking for a quotient between 0.1 and 1.

[Jess in Action][AskingGoodQuestions]
Robert Kausch

Joined: Oct 31, 2006
Posts: 3
Perhaps I'm too optimistic , but, if it can be done in C, as part of the native method, then the operations should be at least possible in Java, right? Definitely wouldn't be as fast, definitely not portable across platforms (or languages for that matter), but it should be possible?

The alternative (slow) way that I came up with is to do the arithmetic by hand, read the sign bit, read the mantissa, read the exponent, call Math.pow(2, exponent), and multiply it by the mantissa and the sign bit, and stuff it into a double / float primitive. Problem is, with the data sets with which I'm dealing are massive (almost Petabytes (not all in memory at once, thank god)), so any slowdown is magnified exponentially.
Robert Kausch

Joined: Oct 31, 2006
Posts: 3
Perhaps I might ask the question another way. Instead of focusing strictly on Java, this is possible in a language, somewhere. In any language in which this is possible, how is this performed?
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
It's possible in Java, but really, by far the easiest way to do it is using Float.floatToIntBits() etc. That's what the method is there for. There's also DataOutputStream which has writeFloat(), but that just calls floatToIntBits() internally. Rather than go into the mathematical details of how to do this without using those methods, I would instead qeustion why you don't want to use them. Float. floatToIntBits() uses the IEEE 754 floating-point standard, which predates Java. As far as I know, C# uses the exact same format. So in all likelihood, you can port this stuff to C# or back with no problems.

Even if for some reason you find you need it in a different format, I strongly suspect that using Float.floatToIntBits() will be a valuable first step, as it translates the float into a known, well-documented format which separates the exponent, mantissa, and sign components of the value. Any subsequent bit-shifting you want to do will be much easier after that, I think.

To use an alternate analogy, let's say you wanted to do integer division, but don't want to use the / operator. Sure, it's possible to write a program in Java or whatever language which will do this for you using the same algorithms we learned in school for long division. If you really, reallyy want to do that, as a learning experience, then OK, fine. But it seems remarkably pointless from any practical standpoint. Much better to just use the tools that you have been provided.

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: Conversion from Floating Point to Binary (not using Wrapper Class Methods)
It's not a secret anymore!