Introduction
The floatToRawIntBits()
method is a static method in the Float
class that returns the integer bits of a floating-point value in accordance with the IEEE 754 floating-point format.
The floatToRawIntBits()
method is a static method in the Float
class that returns the integer bits of a floating-point value in accordance with the IEEE 754 floating-point format.
Firstly, let us create a new file named FloatToRawIntBits.java
in the ~/project
directory using the following command.
touch ~/project/FloatToRawIntBits.java
In this step, you need to import the java.util
and java.lang
packages. Here's the code:
import java.util.*;
import java.lang.*;
In this step, you need to define the FloatToRawIntBits
class. Here's the code:
public class FloatToRawIntBits {
public static void main(String[] args) {
}
}
In this step, you will write code to convert floating values to integer bits using the floatToRawIntBits()
method. Here is the code:
float n1 = 90.85f;
System.out.println("Value in integer bits = " + Float.floatToRawIntBits(n1));
float n2 = n1 / 0.0f;
System.out.println("Value in integer bits = " + Float.floatToRawIntBits(n2));
float n3 = -n1 / 0.0f;
System.out.println("Value in integer bits = " + Float.floatToRawIntBits(n3));
Float n4 = 0.0f / 0.0f;
System.out.println("Value in integer bits = " + Float.floatToRawIntBits(n4));
This code will first define some floating-point values (n1
, n2
, n3
, and n4
). The floatToRawIntBits()
method is then called on each of these values to convert them to integer bits. The resulting integer bit values are then printed to the console using System.out.println()
.
In this step, you will compile and run the code that you wrote in the previous step. Open your terminal, navigate to the ~/project
directory and type the following command.
javac FloatToRawIntBits.java && java FloatToRawIntBits
The output will be as shown below.
Value in integer bits = 1119204147
Value in integer bits = 2139095040
Value in integer bits = -8388608
Value in integer bits = 2143289344
In this step, you will modify the code to take user input. Here is the code:
try {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a floating-point number: ");
float f = sc.nextFloat();
System.out.println("Value in integer bits = " + Float.floatToRawIntBits(f));
} catch (Exception e) {
System.out.println("Invalid input!");
}
This code will use a Scanner
object to get input from the user. It will prompt the user to enter a floating-point number, and then it will call the floatToRawIntBits()
method to convert the input value to integer bits. If the user enters an invalid value, an error message will be printed to the console.
In this step, you will again compile and run the code that you wrote in the previous step. Open your terminal, navigate to the ~/project
directory and type the following command.
javac FloatToRawIntBits.java && java FloatToRawIntBits
The output will be as follows:
Enter a floating-point number: 743.05
Value in integer bits = 1144636211
You can try with different input values and get the respective integer bits.
In this step, you will modify the code to handle cases where the user enters an invalid input. Here is the updated code:
try {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a floating-point number: ");
float f = sc.nextFloat();
System.out.println("Value in integer bits = " + Float.floatToRawIntBits(f));
} catch (InputMismatchException e) {
System.out.println("Invalid input! Please enter a valid floating-point number.");
}
This code will catch InputMismatchException
when invalid input is entered by the user. The catch
block of the code will then print an error message to the console.
In this step, you will compile and run the updated code that you wrote in the previous step. Open your terminal, navigate to the ~/project
directory and type the following command.
javac FloatToRawIntBits.java && java FloatToRawIntBits
The output will be as follows:
Enter a floating-point number: 0x699
Invalid input! Please enter a valid floating-point number.
This shows the error message when the user enters an invalid input.
In this lab, you learned about the floatToRawIntBits()
method in the Java programming language. You learned how to use this method to convert floating-point values to integer bits, how to take user input, and how to handle exceptions. By completing this lab, you should now be able to use the floatToRawIntBits()
method effectively in your own Java programs.