Signup/Sign In

Java Float floatToRawIntBits() method

Java floatToRawIntBits() method is a part of the Float class of the java.lang package. It is a static method that returns the floating-point value of the number passed as argument in accordance with the IEEE 754 floating-point 'single format' bit layout.

Java Float floatToRawintBits() Method example

Java Float floatToRawintBits() Method example

Syntax:

public static int floatToRawIntBits(float value)  

Parameters:

The parameter passed is the float value whose standard integer bits will be returned.

Returns:

Returns the standard integer bits value of the float value passed as an argument. Also the integer bit values of 0x7f800000 and 0xff800000 for positive and negative infinity values respectively. For NaN value, this method returns the actual integer value of NaN. Unlike floatToIntBits() method, it does not collapse all the bit patterns encoding a NaN to a single canonical NaN value.

Example 1:

Here, some random floating values are taken and respective int bits are returned.

import java.lang.Float;

public class StudyTonight 
{  
    public static void main(String[] args)
    {         
        float n1 = 90.85f;  
        System.out.println(" value in int Bits = "+ Float.floatToRawIntBits(n1)); //float value converted into int bits 
        
        float n2 = n1/0.0f;  
        System.out.println(" value in int Bits = "+Float.floatToRawIntBits(n2));  //float value as positive infinity
          
        float n3 = -n1/0.0f;  //argument is negative infinity 
        System.out.println(" value in int Bits = "+Float.floatToRawIntBits(n3));  
         
        Float n4 = 0.0f/0.0f; //argument is NaN
        System.out.println(" value in int Bits = "+Float.floatToRawIntBits(n4)); 
  
    }  
} 


value in int Bits = 1119204147
value in int Bits = 2139095040
value in int Bits = -8388608
value in int Bits = 2143289344

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.

import java.lang.Float;
import java.util.Scanner;

public class StudyTonight 
{  
    public static void main(String[] args)
    {  
        try
        {
            System.out.println("Enter value");
            Scanner sc = new Scanner(System.in);
            float f = sc.nextFloat();
            System.out.println(" value in int Bits = "+ Float.floatToRawIntBits(f)); //float value converted into int bits 
        }
        catch(Exception e)
        {
          System.out.println("Invalid Input!!");
        } 
   }  
}  


Enter value
743.05
value in int Bits = 1144636211
*******************************
Enter value
NaN
value in int Bits = 2143289344
*******************************
Enter value
0x699
Invalid Input!!

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.