Signup/Sign In
PUBLISHED ON: AUGUST 26, 2021

Java Double doubleToRawLongBits() method

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

Syntax:

public static long doubleToRawLongBits(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.

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.doubleToRawLongBits(n1)); //double value converted into long bits        
        double n2 = n1/0.0;  
        System.out.println(" value in long Bits = "+Double.doubleToRawLongBits(n2));  //double value as positive infinity          
        double n3 = -n1/0.0;  //argument is negative infinity 
        System.out.println(" value in long Bits = "+Double.doubleToRawLongBits(n3));           
        double n4 = 0.0/0.0; //argument is NaN
        System.out.println(" value in long Bits = "+Double.doubleToRawLongBits(n4));   
    }  
}


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

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.doubleToRawLongBits(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.88
value in long Bits = 4633061228158795121
******************************************************
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.