Signup/Sign In

Java Float floatToIntBits() Method

Java floatToIntBits() 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.

According to IEEE 754 floating-point representation, a 32 bit and a 64-bit floating number can be represented as below:

Java Float floatToIntBits() Method example

Java Float floatToIntBits() Method

Syntax:

public static int floatToIntBits(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,0xff800000 and 0x7fc00000 are returned in case the value of parameter is positive infinity,negative infinity and NaN respectively.

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.floatToIntBits(n1)); //float value converted into int bits 

		float n2 = n1/0.0f;  
		System.out.println(" value in int Bits = "+Float.floatToIntBits(n2));  //float value as positive infinity

		float n3 = -n1/0.0f;  //argument is negative infinity 
		System.out.println(" value in int Bits = "+Float.floatToIntBits(n3));  

	}

}

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

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.floatToIntBits(f)); //float value converted into int bits 
        }
        catch(Exception e)
        {
          System.out.println("Invalid Input!!");
        }  
   }  
}  


Enter value
89.3213
value in int Bits = 1119003777
***********************************
Enter value
-45.66
value in int Bits = -1036606505
************************************
Enter value
76x00
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.