如何在屏幕上显示二进制的float和double? 格式: float: s x*8 m*23double: s x*11 m*52s是符号位,x是指数位,m是尾数位恳请指教.. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 对float来说,把数字跟0x8000做按位与操作,即“&”,如果结果等于0x8000,就输出1,否则输出0,然后把数字左移一位,再跟0x8000做按位与操作, 同样如果结果等于0x8000,就在同一行输出1,否则输出0,循环32次。对double来说,跟0x8000000做按位与,循环64次 0x8000和float或double不让 & 操作... ……这种东西在JAVA里没弄过,以前在C里这么用的。 转换 double 和 float 成为计算机内部的表示形式,在 Java 中主要用到 Double.doubleToLongBits(),即将一个 double 型的数转换成相同参数的 long 型数据,同理, Float.floatToIntBits() ,将一个 float 型的数转换成相同参数的 int 型数据。因为 long 与 double 和 int 与 float 在计算机内部所占用的位数是相同的。根据 IEEE 754 的标准,浮点数的计算机内部表示除了符号位、指数位和尾数位之外,还有一个隐含位(不在计算机内部表示),这一位需要通过某些算法进行计算而得出的,这里就从略了。public static void main(String[] args) { double pi = Math.PI; float s = 3.5454f; char[] d = toCharBits(Double.doubleToLongBits(pi), 64); System.out.print(pi + ": "); outputBits(d); char[] f = toCharBits(Float.floatToIntBits(s), 32); System.out.print(s + ": "); outputBits(f);}// 在转换成二进制表示时,不采用 Long.toBinaryString() 和 Integer.toBinaryString(),// 因为这两个方法会略去高位为“0”的数据,造成位数遗失,// 为了避免出现位数不足的情况,实现将其转为字符数组,// 以便于进行其他操作(比如:分解符号位、指数位、数值位和分析隐含位等操作)public static char[] toCharBits(long num, int size) { char bits[] = new char[size]; for (int i = size-1; i >= 0; --i) { bits[i] = (num & 1) == 0 ? '0' : '1'; num >>>= 1; } return bits;}public static void outputBits(char[] charBits){ for(int i = 0; i < charBits.length; i++){ System.out.print(charBits[i]); } System.out.println();} class Lesson22{ public static void main(String[] args) { float g=3; int h=Float.floatToIntBits(g); System.out.println(h); String i=Integer.toBinaryString(h); System.out.println(i); System.out.println("00000000000000000000000000000000");//打印32个0,以便和i的位数比较. Integer j=Integer.parseInt(i,2); System.out.println(j); float k=Float.intBitsToFloat(j.intValue()); System.out.println(k); System.out.println(Float.intBitsToFloat(0x40400000) );//这个0x40400000就是上面打印出来的100000001000000000000000000 }} 字符串替换,正则表达式 排序 在线等 g.drawImage的参数看不懂 怎么错了阿!!!!!!!!!! java与mysql做连接时‘JDBC驱动’的环境变量该怎么配置 为什么我的路径设置晚了不好使? 怎样实现文本域的左对齐,右对齐,居中对齐? 请问这句话是什么意思??——类、this、实例 非常着急的问题 这两个例子并没有编译出错啊,为什么SCJP的答案说编译出错??? 急求:一个小程序的设计. 怎么把零散的年、月、日变量赋值给date对象。
double pi = Math.PI;
float s = 3.5454f;
char[] d = toCharBits(Double.doubleToLongBits(pi), 64);
System.out.print(pi + ": ");
outputBits(d);
char[] f = toCharBits(Float.floatToIntBits(s), 32);
System.out.print(s + ": ");
outputBits(f);
}// 在转换成二进制表示时,不采用 Long.toBinaryString() 和 Integer.toBinaryString(),
// 因为这两个方法会略去高位为“0”的数据,造成位数遗失,
// 为了避免出现位数不足的情况,实现将其转为字符数组,
// 以便于进行其他操作(比如:分解符号位、指数位、数值位和分析隐含位等操作)
public static char[] toCharBits(long num, int size) {
char bits[] = new char[size];
for (int i = size-1; i >= 0; --i) {
bits[i] = (num & 1) == 0 ? '0' : '1';
num >>>= 1;
}
return bits;
}public static void outputBits(char[] charBits){
for(int i = 0; i < charBits.length; i++){
System.out.print(charBits[i]);
}
System.out.println();
}
{
public static void main(String[] args)
{
float g=3;
int h=Float.floatToIntBits(g);
System.out.println(h);
String i=Integer.toBinaryString(h);
System.out.println(i);
System.out.println("00000000000000000000000000000000");//打印32个0,以便和i的位数比较.
Integer j=Integer.parseInt(i,2);
System.out.println(j);
float k=Float.intBitsToFloat(j.intValue());
System.out.println(k);
System.out.println(Float.intBitsToFloat(0x40400000) );//这个0x40400000就是上面打印出来的100000001000000000000000000
}
}