Home » Java Float floatToRawIntBits() method with Examples

Java Float floatToRawIntBits() method with Examples

by Online Tutorials Library

Java Float floatToRawIntBits() method

The floatToRawIntBits() method of Java Float class returns the specified floating-point value according to the IEEE 754 floating-point ‘single format’ bit layout.

Syntax:

Parameters:

A float type parameter (value) is passed. Here value is a floating-point number.

Return Value:

The floatToRawIntBits(float value) method of Float class returns the bits of the given floating-point number.

  • If positive infinity is passed as an argument, it will return 0x7f800000.
  • If negative infinity is passed as an argument, it will return 0xff800000.
  • If NaN is passed as an argument, it will return the actual integer of NaN value. It is different from floatIntBits() method as this method does not collapse all the bits patterns encoding a NaN to a single canonical Nan value.

Example 1

Test it Now

Output:

9.8 value in  Raw int bits = 1092406477  NaN value in Raw int bits = 2143289344  Infinity value in Raw int bits = 2139095040  -Infinity value in Raw int bits = -8388608  

Example 2

Test it Now

Output:

-658756.56value in Raw long bits = -920595383  -6547.6577 value in Raw long bits = -976446141  

Example 3

Test it Now

Output:

Error:(14, 42) java: incompatible types: int cannot be converted to java.lang.Float  

Here, we are converting it into int bits and then putting it into float without an explicit cast. Either we should type cast it or should directly call this method in println function otherwise we will get an error.

Next TopicJava Float

You may also like