Float floatToRawIntBits() method in Java with examples
Last Updated :
26 Oct, 2018
The floatToRawIntBits() method in Float Class is a built-in function in java that returns a representation of the specified floating-point value according to the IEEE 754 floating-point “single format” bit layout, preserving Not-a-Number (NaN) values.
Syntax:
public static int floatToRawIntBits(float val)
Parameter: The method accepts only one parameter val which specifies a floating-point number.
Return Values: The function returns the bits that represent the floating-point number. However there are 3 special cases:
- If the argument is positive infinity, the result is 0x7f800000.
- If the argument is negative infinity, the result is 0xff800000.
- If the argument is NaN, the result is 0x7fc00000.
Below programs illustrates the use of Float.floatToRawIntBits() method:
Program 1:
import java.lang.*;
class Gfg1 {
public static void main(String args[])
{
float val = 1 .5f;
int answer = Float.floatToRawIntBits(val);
System.out.println(val + " in raw int bits: "
+ answer);
}
}
|
Output:
1.5 in raw int bits: 1069547520
Program 2:
import java.lang.*;
class Gfg1 {
public static void main(String args[])
{
float val = Float.POSITIVE_INFINITY;
float val1 = Float.NEGATIVE_INFINITY;
float val2 = Float.NaN;
int answer = Float.floatToRawIntBits(val);
System.out.println(val + " in raw int bits: "
+ answer);
answer = Float.floatToRawIntBits(val1);
System.out.println(val1 + " in raw int bits: "
+ answer);
answer = Float.floatToRawIntBits(val);
System.out.println(val2 + " in raw int bits: "
+ answer);
}
}
|
Output:
Infinity in raw int bits: 2139095040
-Infinity in raw int bits: -8388608
NaN in raw int bits: 2139095040