Thursday, July 4, 2024
HomeLanguagesJavaFloat floatToIntBits() method in Java with examples

Float floatToIntBits() method in Java with examples

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

Syntax:

public static int floatToIntBits(float val)

Parameter: The method accepts only one parameter val which specifies a floating-point number to be converted into integer bits.

Return Values: The function returns the integer bits that represent the floating-point number. Below are the 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.floatToIntBits() method:

Program 1:




// Java program to demonstrate
// Float.floatToIntBits() method
import java.lang.*;
  
class Gfg1 {
  
    public static void main(String args[])
    {
  
        float val = 1.5f;
  
        // function call
        int answer = Float.floatToIntBits(val);
  
        // print
        System.out.println(val + " in int bits: "
                           + answer);
    }
}


Output:

1.5 in int bits: 1069547520

Program 2:




// Java program to demonstrate
// Float.floatToIntBits() method
  
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;
  
        // function call
        int answer = Float.floatToIntBits(val);
  
        // print
        System.out.println(val + " in int bits: "
                           + answer);
  
        // function call
        answer = Float.floatToIntBits(val1);
  
        // print
        System.out.println(val1 + " in int bits: "
                           + answer);
  
        // function call
        answer = Float.floatToIntBits(val);
  
        // print
        System.out.println(val2 + " in int bits: "
                           + answer);
    }
}


Output:

Infinity in int bits: 2139095040
-Infinity in int bits: -8388608
NaN in int bits: 2139095040

Reference: https://docs.oracle.com/javase/7/docs/api/java/lang/Float.html#floatToIntBits(float)

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments