求 float数据在内存中的存储方法 的 java PHP C/C++ 程序

可以看我这个日志将计算方法的 http://www.cnblogs.com/shangbolei/p/4441946.html

用PHP写的

function float_bin($shang)
{    
//保留6位小数的浮点型 保留更多位的更改 
$x = decbin($shang * 64 );     //把小数位换成整数
$y = $x /1000000 ;     //还原小数位
for($i = 0; $y > 2 ; $i++)     //求右移位数 i
$y = $y/10;    
$xiaoshu = str_split($y-1,1);    
$zheng = decbin($i+127);     //得出 E 的值
$shu = '0'.$zheng;     //S 位 为整数 为 0 负数为 1 
for($j=2; $j<count($xiaoshu) ; $j++)    //S E M 的数值部分拼接
$shu = $shu.$xiaoshu[$j];    
while(strlen($shu)< 32)     //不足32位 补 0
$shu = $shu.'0';    
$shu_float = base_convert($shu,2,16);    //2进制转化成16进制
return $shu_float;
}

用C/C++写的

#include<iostream>
#include<stdio.h>
#define uchar unsigned char
using namespace std;
void binary_print(uchar c)
{
    for (int i = 0; i < 8; ++i)
    {    if ((c << i) & 0x80)
            cout << '1';
        else
            cout << '0';    }
    cout << ' ';
}
void main()
{
    float a;
    uchar c_save[4];
    uchar i;
    void *f;
    f = &a;
    cout << "请输入一个浮点数:";
    cin >> a;
    cout << endl;
    for (i = 0; i<4; i++)
        c_save[i] = *((uchar*)f + i);
    cout << "此浮点数在计算机内存中储存格式如下:" << endl;
    for (i = 4; i != 0; i--)
        binary_print(c_save[i - 1]);
    cout << endl;
    system("pause");
}

用JAVA写的

public class Hello {

    public static void main(String[] args) {
        float f = 12.25f;
        // float数据在内存中的存储方法
        System.out.println(Integer.toHexString(Float.floatToIntBits(f)));
        //从内存转化过来
        System.out.println(Float.intBitsToFloat(Integer.parseInt("41440000", 16)));
    }

}
原文地址:https://www.cnblogs.com/shangbolei/p/4443715.html