Java Reference
In-Depth Information
if(t != 0)
System.out.print(“1");
else
System.out.print(“0");
m=m<1;
}
System.out.println(“\n ^ |”);
System.out.println(“implicit bit -| | ——- 52 bits —>”);
}
}
When the program executes with an input of -127.375 the following out-
put is produced:
As long = c05fd80000000000
Sign = -
Exponent = 4050000000000000
Significand = fd80000000000
Fields in binary
Sign bit = 1
Exponent = 10000000101
|-11 bits-|
Significand = 1.11111101100000000000000000000000000000000000000000
^|
implicit bit -| | ——— 52 bits —>
On the Web
The program BitOps.java is located in the Chapter 24 folder at
www.crcpress.com .
Conversion Operations
Code often needs to convert a floating-point value into an integer. One
method is by typecasting, for example:
double a = 1.998899;
long b = (long) a;
//b=1
In this case Java truncates the integer portion of the double variable
and uses it for the value of the integer variable. In some cases this conver-
sion may be acceptable, but more often you need to obtain the value of
the integer that is closest to the floating-point number. The round()
method of java.lang.Math returns the closest double to the argument if
the argument is a double, and the closest int if the argument is a float. The
round() method was described in Chapter 23. The following fragment
shows the use of the round() method:
Search WWH ::




Custom Search