Even though you may know about IEEE floating point numbers, you've probably never dealt with them on a bit-by-bit basis. I'll show you how, the next time your Java project needs to create floating point numbers from a series of bits or create a sequence of bits from a floating point number, the Float class can do the work for you.

A refresher on IEEE floating point numbers

The IEEE 754 floating-point single precision number format defines a bit layout for storing floating point numbers. One bit is set aside for the sign, eight bits are set aside for the exponent, and 23 bits are set aside for the mantissa. These bits are laid out in order from the most significant bit to the least significant as illustrated below:

31 0 | | SEEEEEEEEMMMMMMMMMMMMMMMMMMMMMMM where: S = sign bit E = exponent M = mantissa

You can purchase a complete explanation of IEEE floating point numbers from IEEE's site. (If you search online for IEEE 754, you should be able to find what you need for free.)

Float's contribution to the Java community

The sample program below uses two of Float's methods to convert a float to bits and those bits back to a float.

To convert a float value to a sequence of bits, use Float.floatToIntBits(float f). This method returns a 32-bit integer representing the IEEE 754 arranged bits of the floating point number you supply as an argument.

To move in the opposite direction, you use the Float.intBitsToFloat(int bits) method. This method takes the integer passed and unpacks the bits into an IEEE floating point number.

Here's the sample program:


public class BitsTip { 
  public static void main(String args[]) { 
  float f = Float.parseFloat(args[0]); 
  int bits = Float.floatToIntBits(f);

  System.out.println("bits: " + bits); 
  System.out.println("back to float: " + Float.intBitsToFloat(bits)); 
  } 
}

If you've ever had to code this conversion by hand, then you'll appreciate the work these two simple methods save you. If you're dealing with 64-bit numbers, then check out the Double wrapper class. It has the same methods for IEEE 754 double precision numbers.

Be sure to read the javadoc for any gotchas and to get a complete idea of what these methods can do.

Interpreting Java This was published in Interpreting Java, check every Tuesday for more stories

Related links

Comments

1

Bill - 11/12/08

Hello,

Java is a kind of computer language and it is quite helpful in saving time while working.
Bill
[url=" http://www.casualdate.net.au"rel="DoFollow"]Online Dating[/url]

» Report offensive content

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

1

Bill - 12/11/08

Hello, Java is a kind of computer language and it is quite helpful in saving time while working. Bill [url=" http://www.casualdate.net.au"rel="DoFollow"]Online Dating[/url] ... more

Log in


Sign up | Forgot your password?

  • Staff Aussies to pay more for Win 7

    If you are looking to make some money in these troubled times, perhaps importing copies of Windows 7 could be for you. Read more »

    -- posted by Staff

  • Staff Firefox: Greens want it, 3.5rc2 not up to par

    This week's roundup looks at the situation surrounding a campaign to change Outlook HTML renderer, a Greens MP wants to install Firefox but is restricted and all the photos from the iPhone 3GS launch. Read more »

    -- posted by Staff

  • Chris Duckett Microsoft misses the Outlook point

    Ask designers which mail program is the bane of their existence, and you'll find that Outlook tops the list. The reason why the most popular email reader is also the most painful is simple: it uses Word to render HTML emails. Read more »

    -- posted by Chris Duckett

What's on?