Java DoubleToLongBits Method

JavaJavaBeginner
Practice Now

Introduction

The Double.doubleToLongBits() method is used to get the standard long bits value of the double value passed as an argument in accordance with the IEEE 754 floating-point 'double format' bit layout. This method comes with the Double class of the java.lang package. It is a static method that returns long.

Add necessary code

Add the following code to the DoubleToLongBits.java file to ask the user to enter a double value and print the long bits value of the entered double value.

import java.util.Scanner;
import java.lang.Double;

public class DoubleToLongBits {
    public static void main(String[] args) {
        System.out.println("Enter a double value: ");
        Scanner input = new Scanner(System.in);
        double val = input.nextDouble();

        // get the standard long bits value of the entered double value
        long bits = Double.doubleToLongBits(val);
        System.out.println("Standard long bits value of " + val + " is: " + bits);
    }
}

Compile and run the code

To compile the source code, open the terminal and navigate to the directory where the DoubleToLongBits.java file is located. Then, type the following command:

javac DoubleToLongBits.java

Next, to run the compiled DoubleToLongBits program, execute the following command:

java DoubleToLongBits

When you run the program, it asks you to enter a double value. When you enter the value, it prints the standard long bits value of the entered double value to the console.

Testing with sample inputs

Now that the program is functional, you can test it by entering different double values. Here are some sample inputs and their respective outputs:

Input:
Enter a double value:
56.78
Output:
Standard long bits value of 56.78 is: 4633440770209674064
Input:
Enter a double value:
0.99
Output:
Standard long bits value of 0.99 is: 4616189618054758408
Input:
Enter a double value:
-5.77
Output:
Standard long bits value of -5.77 is: -4642405335153096998

Summary

In this lab, you learned how to use the Java Double.doubleToLongBits() method, which is used to get the standard long bits value of the double value passed as an argument. You learned about the syntax, parameters, and return values of the method. Furthermore, you also saw some examples of using the Double.doubleToLongBits() method in a Java program to convert a double value to its standard long bits value.

Other Java Tutorials you may like