Signup/Sign In
PUBLISHED ON: AUGUST 26, 2021

Java Double doubleToLongBits() Method

Java doubleToLongBits() method is a part of the Double class of the java.lang package. It is a static method that returns the long value of the number passed as an argument in accordance with the IEEE 754 floating-point 'double format' bit layout.

Syntax:

public static long doubleToLongBits(double value)  

Parameters:

The parameter passed is the double value whose standard long bits will be returned.

Returns:

Returns the standard long bits value of the double value passed as an argument.

Example 1:

Here, some random double values are taken and respective long bits are returned using the doubleToLongBits() method.

import java.lang.Double;
public class StudyTonight 
{
	public static void main(String[] args) 
	{
		double n1 = 90.85;  
		System.out.println(" value in long Bits = "+ Double.doubleToLongBits(n1)); //double value converted into long bits 

		double n2 = n1/0.0;  
		System.out.println(" value in long Bits = "+Double.doubleToLongBits(n2));  //double value as positive infinity
		double n3 = -n1/0.0;  //argument is negative infinity 
		System.out.println(" value in long Bits = "+Double.doubleToLongBits(n3));  
	}
}


value in long Bits = 4636093417345410662
value in long Bits = 9218868437227405312
value in long Bits = -4503599627370496

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.Double;
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);
            double d = sc.nextDouble();
            System.out.println(" value in long Bits = "+ Double.doubleToLongBits(d)); //double value converted into long bits 
        }
        catch(Exception e)
        {
          System.out.println("Invalid Input!!");
        }  
   }  
}  


Enter value
NaN
value in long Bits = 9221120237041090560
*********************************************
Enter value
55.98
value in long Bits = 4633075301907630653
*********************************************
Enter value
0x588
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.