格式: 
float: s  x*8  m*23
double: s  x*11 m*52
s是符号位,x是指数位,m是尾数位
恳请指教..

解决方案 »

  1.   

    对float来说,把数字跟0x8000做按位与操作,即“&”,如果结果等于0x8000,就输出1,否则输出0,然后把数字左移一位,再跟0x8000做按位与操作, 同样如果结果等于0x8000,就在同一行输出1,否则输出0,循环32次。对double来说,跟0x8000000做按位与,循环64次
      

  2.   

    0x8000和float或double不让 & 操作...
      

  3.   

    ……这种东西在JAVA里没弄过,以前在C里这么用的。
      

  4.   

    转换 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();
    }
      

  5.   

    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
    }
    }