Java - Float Class intBitsToFloat() Method

Float class intBitsToFloat() method: Here, we are going to learn about the intBitsToFloat() method of Float class with its syntax and example. By Preeti Jain Last updated : March 21, 2024

Float class intBitsToFloat() method

  • intBitsToFloat() method is available in java.lang package.
  • intBitsToFloat() method follows IEEE 754 floating-point standards and according to standards, it returns the float value corresponding to a given argument that denotes integer bits representation.
  • intBitsToFloat() method is a static method, it is accessible with the class name too and if we try to access the method with the class object then also we will not get an error.
  • intBitsToFloat() method does not throw an exception at the time of converting bits representations to float value.

Syntax

public static float intBitsToFloat(int bits_rep);

Parameters

  • int bits_rep – represents the integer value in bits.

Return Value

The return type of this method is float, it returns the float value that represent the given argument in integer bits.

  • If we pass "0x7f800000", it returns the value "positive infinity".
  • If we pass "0xff800000", it returns the value "negative infinity".
  • If value lies in between "0x7f800001" and "0x7fffffff" or the value lies in between "0xff800001" and "0xffffffff".

Example

// Java program to demonstrate the example 
// of intBitsToFloat (int bits_rep)
// method of Float class

public class IntBitsToFloatOfFloatClass {
    public static void main(String[] args) {
        // Variables initialization
        int value1 = 20;
        int value2 = 0x7f800000;
        int value3 = 0xff800000;

        // Display value1,value2,value3 values
        System.out.println("value1: " + value1);
        System.out.println("value2: " + value2);
        System.out.println("value3: " + value3);


        // It returns the float value denoted by the given 
        // bit representation by calling Float.intBitsToFloat(value1)
        float result1 = Float.intBitsToFloat(value1);

        // It returns the float value denoted by the given 
        // bit representation by calling Float.intBitsToFloat(value2)
        float result2 = Float.intBitsToFloat(value2);

        // It returns the float value denoted by the given 
        // bit representation by calling Float.intBitsToFloat(value3)
        float result3 = Float.intBitsToFloat(value3);

        // Display result1,result2, result3 values
        System.out.println("Float.intBitsToFloat(value1): " + result1);
        System.out.println("Float.intBitsToFloat(value2): " + result2);
        System.out.println("Float.intBitsToFloat(value3): " + result3);
    }
}

Output

value1: 20
value2: 2139095040
value3: -8388608
Float.intBitsToFloat(value1): 2.8E-44
Float.intBitsToFloat(value2): Infinity
Float.intBitsToFloat(value3): -Infinity

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.