# Gleitkommazahlen in 32-Bit-Codierung überführen

D

#### diwinter

Hallo,
folgendes (verständnis-)Problem tut sich bei mir auf: wenn ich die Gleitkommazahl (typ float) -0,2509765624 oder 0,8 in eine 32-Bit-Codierung überführen möchte, wie muß ich da herangehen? Bei Zahlen über 1 ist das alles kein Problem für mich, aber ab da verstehe ich das nicht mehr.
Auch habe ich mich gefragt, wie viele verschiedene numerische Werte in den Darstellungen float und dpuble dargestellt werden können?

Wäre schön, wenn ihr mir helfen könnt.
Vielen Dank im voraus!

#### Thomas Darimont

##### Erfahrenes Mitglied
Hallo!

Also in der Beschreibung zu int Float.floatToIntBits(float f); steht folgendes:
Code:
`````` /**
* Returns the <code>float</code> value corresponding to a given
* bit representation.
* The argument is considered to be a representation of a
* floating-point value according to the IEEE 754 floating-point
* "single format" bit layout.
* <p>
* If the argument is <code>0x7f800000</code>, the result is positive
* infinity.
* <p>
* If the argument is <code>0xff800000</code>, the result is negative
* infinity.
* <p>
* If the argument is any value in the range
* <code>0x7f800001</code> through <code>0x7fffffff</code> or in
* the range <code>0xff800001</code> through
* <code>0xffffffff</code>, the result is a NaN.  No IEEE 754
* floating-point operation provided by Java can distinguish
* between two NaN values of the same type with different bit
* patterns.  Distinct values of NaN are only distinguishable by
* use of the <code>Float.floatToRawIntBits</code> method.
* <p>
* In all other cases, let <i>s</i>, <i>e</i>, and <i>m</i> be three
* values that can be computed from the argument:
* <blockquote><pre>
* int s = ((bits &gt;&gt; 31) == 0) ? 1 : -1;
* int e = ((bits &gt;&gt; 23) & 0xff);
* int m = (e == 0) ?
*                 (bits & 0x7fffff) &lt;&lt; 1 :
*                 (bits & 0x7fffff) | 0x800000;
* </pre></blockquote>
* Then the floating-point result equals the value of the mathematical
* expression <i>s</i>&middot;<i>m</i>&middot;2<sup><i>e</i>-150</sup>.
*<p>
* Note that this method may not be able to return a
* <code>float</code> NaN with exactly same bit pattern as the
* <code>int</code> argument.  IEEE 754 distinguishes between two
* kinds of NaNs, quiet NaNs and <i>signaling NaNs</i>.  The
* differences between the two kinds of NaN are generally not
* visible in Java.  Arithmetic operations on signaling NaNs turn
* them into quiet NaNs with a different, but often similar, bit
* pattern.  However, on some processors merely copying a
* signaling NaN also performs that conversion.  In particular,
* copying a signaling NaN to return it to the calling method may
* perform this conversion.  So <code>intBitsToFloat</code> may
* not be able to return a <code>float</code> with a signaling NaN
* bit pattern.  Consequently, for some <code>int</code> values,
* <code>floatToRawIntBits(intBitsToFloat(start))</code> may
* <i>not</i> equal <code>start</code>.  Moreover, which
* particular bit patterns represent signaling NaNs is platform
* dependent; although all NaN bit patterns, quiet or signaling,
* must be in the NaN range identified above.
*
* @param   bits   an integer.
* @return  the <code>float</code> floating-point value with the same bit
*          pattern.
*/
public static native float intBitsToFloat(int bits);``````

Beispiel:
Code:
``````package de.tutorials;

public class FloatToIntTest {

public static void main(String[] args) {
float f = -0.2509765624F;

int bits = Float.floatToIntBits(f);

System.out.println(Integer.toBinaryString(bits));

float f0 = Float.intBitsToFloat(bits);

System.out.println(f0);
}
}``````

HTH
Gruß Tom