Double doubleToLongBits() method in Java with examples

The java.lang.Double.doubleToLongBits() method of Java Double 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 “double format” bit layout.

Syntax:

public static long doubleToLongBits(double val)

Parameter: The method accepts only one parameter val which specifies a double precision floating-point number.



Return Values: The function returns the bits that represent the floating-point number. Below are the special cases:

  • If the argument is positive infinity, the result is 7ff0000000000000L.
  • If the argument is negative infinity, the result is 0xfff0000000000000L.
  • If the argument is NaN, the result is 0x7ff8000000000000L.

Below programs illustrates the use of java.lang.Double.doubleToLongBits() method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

1.5 in long bits: 4609434218613702656

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Double.doubleToLongBits() method
  
import java.lang.*;
  
class Gfg1 {
  
    public static void main(String args[])
    {
  
        double val = Double.POSITIVE_INFINITY;
        double val1 = Double.NEGATIVE_INFINITY;
        double val2 = Double.NaN;
  
        // function call
        long answer = Double.doubleToLongBits(val);
  
        // print
        System.out.println(val + " in long bits: "
                           + answer);
  
        // function call
        answer = Double.doubleToLongBits(val1);
  
        // print
        System.out.println(val1 + " in long bits: "
                           + answer);
  
        // function call
        answer = Double.doubleToLongBits(val2);
  
        // print
        System.out.println(val2 + " in long bits: "
                           + answer);
    }
}

chevron_right


Output:

Infinity in long bits: 9218868437227405312
-Infinity in long bits: -4503599627370496
NaN in long bits: 9221120237041090560

Reference: https://docs.oracle.com/javase/7/docs/api/java/lang/Double.html#doubleToLongBits(double)



My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.