// I3e.cpp : Defines the entry point for the console application. // // Floating-point numbers use the IEEE (Institute of Electrical and Electronics Engineers) format. // Single-precision values with float type have 4 bytes, consisting of a sign bit, // an 8-bit excess-127 binary exponent, and a 23-bit mantissa. // The mantissa represents a number between 1.0 and 2.0. // Since the high-order bit of the mantissa is always 1, // it is not stored in the number. // You can declare variables as float or double, depending on the needs of your // application. The principal differences between the two types are // the significance they can represent, the storage they require, and their range. // Table 3.3 shows the relationship between significance and storage requirements.// Table 3.3: // Type Significant digits,Number of bytes // float 6 –7 4 // double 15–16 8 // Floating-point variables are represented by a mantissa, // which contains the value of the number, and an exponent, // which contains the order of magnitude of the number. // Table 3.4 shows the number of bits allocated to the mantissa // and the exponent for each floating-point type. The most significant bit // of any float or double is always the sign bit. If it is 1, the number is // considered negative; otherwise, it is considered a positive number. // Table 3.4 : // Type Exponent length,Mantissa length // float 8 bits 23 bits // double 11 bits 52 bits // Because exponents are stored in an unsigned form, // the exponent is biased by half its possible value. For type float, // the bias is 127; for type double, it is 1023. You can compute the // actual exponent value by subtracting the bias value from the exponent value. // The mantissa is stored as a binary fraction greater than or equal to 1 // and less than 2. For types float and double, there is an implied leading 1 // in the mantissa in the most-significant bit position, so the mantissas are // actually 24 and 53 bits long, respectively, even though the most-significant bit // is never stored in memory.// Intel IEEE 格式: S(1bit) E(8bit) M(23bit) // Microsoft MBF 格式: E(8bit) S(1bit) M(23bit)#include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <string.h>int main(int argc, char* argv[]) { union IEEE { float f;// 微软浮点格式.与 INTEL 浮点格式(IEEE)不同 int i; unsigned int dw;// 2 words } ; IEEE xxx; char bit[80]; // int *P; // P=(int*)0x100; for (int jj=0;jj<10;jj++) { xxx.f=(float)jj/8;// change this for other data itoa(xxx.i,bit,2);//整数转换为二进制字串 strrev(bit);// 翻转字串 unsigned int len=strlen(bit); for (int jj=0;jj<(int)(32-len);jj++) {// 补零 strcat(bit,"0"); } strrev(bit);// 恢复 printf("%f = %32s = %08X\n",xxx.f,bit,xxx.i); // 再计算回来 if(xxx.f!=0) {// xxx.dw ==0 时, 规定 xxx.f 为0 char sign=xxx.dw & 0x80000000 ? '-' : '+';// 最高位是符号位 int exp=(xxx.dw & 0x7F800000)>>23;//以后8位是指数部分,要减127 float mantissa=(float)(xxx.dw & 0x007FFFFF)/0x007FFFFF+1;//+1 隐藏的 printf("Sign=%C,Exponent=2^%d,mantissa=%f\n",sign,exp-127,mantissa); } } // float y=0.0; for(;;) { printf ("输入一个浮点数, 输入字符退出.\n"); if (scanf("%f",&y)==0) return 1; xxx.f=(float)y;// change this for other data itoa(xxx.i,bit,2);//整数转换为二进制字串 strrev(bit);// 翻转字串 unsigned int len=strlen(bit); for (int jj=0;jj<(int)(32-len);jj++) {// 补零 strcat(bit,"0"); } strrev(bit);// 恢复 printf("%f = %32s = %08X\n",xxx.f,bit,xxx.i); } // return 0; }
//
// Floating-point numbers use the IEEE (Institute of Electrical and Electronics Engineers) format.
// Single-precision values with float type have 4 bytes, consisting of a sign bit,
// an 8-bit excess-127 binary exponent, and a 23-bit mantissa.
// The mantissa represents a number between 1.0 and 2.0.
// Since the high-order bit of the mantissa is always 1,
// it is not stored in the number.
// You can declare variables as float or double, depending on the needs of your
// application. The principal differences between the two types are
// the significance they can represent, the storage they require, and their range.
// Table 3.3 shows the relationship between significance and storage requirements.// Table 3.3:
// Type Significant digits,Number of bytes
// float 6 –7 4
// double 15–16 8 // Floating-point variables are represented by a mantissa,
// which contains the value of the number, and an exponent,
// which contains the order of magnitude of the number.
// Table 3.4 shows the number of bits allocated to the mantissa
// and the exponent for each floating-point type. The most significant bit
// of any float or double is always the sign bit. If it is 1, the number is
// considered negative; otherwise, it is considered a positive number.
// Table 3.4 :
// Type Exponent length,Mantissa length
// float 8 bits 23 bits
// double 11 bits 52 bits // Because exponents are stored in an unsigned form,
// the exponent is biased by half its possible value. For type float,
// the bias is 127; for type double, it is 1023. You can compute the
// actual exponent value by subtracting the bias value from the exponent value.
// The mantissa is stored as a binary fraction greater than or equal to 1
// and less than 2. For types float and double, there is an implied leading 1
// in the mantissa in the most-significant bit position, so the mantissas are
// actually 24 and 53 bits long, respectively, even though the most-significant bit
// is never stored in memory.// Intel IEEE 格式: S(1bit) E(8bit) M(23bit)
// Microsoft MBF 格式: E(8bit) S(1bit) M(23bit)#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>int main(int argc, char* argv[])
{
union IEEE
{
float f;// 微软浮点格式.与 INTEL 浮点格式(IEEE)不同
int i;
unsigned int dw;// 2 words
} ;
IEEE xxx;
char bit[80];
// int *P;
// P=(int*)0x100;
for (int jj=0;jj<10;jj++)
{
xxx.f=(float)jj/8;// change this for other data
itoa(xxx.i,bit,2);//整数转换为二进制字串
strrev(bit);// 翻转字串
unsigned int len=strlen(bit);
for (int jj=0;jj<(int)(32-len);jj++)
{// 补零
strcat(bit,"0");
}
strrev(bit);// 恢复
printf("%f = %32s = %08X\n",xxx.f,bit,xxx.i);
// 再计算回来
if(xxx.f!=0)
{// xxx.dw ==0 时, 规定 xxx.f 为0
char sign=xxx.dw & 0x80000000 ? '-' : '+';// 最高位是符号位
int exp=(xxx.dw & 0x7F800000)>>23;//以后8位是指数部分,要减127
float mantissa=(float)(xxx.dw & 0x007FFFFF)/0x007FFFFF+1;//+1 隐藏的
printf("Sign=%C,Exponent=2^%d,mantissa=%f\n",sign,exp-127,mantissa);
}
}
//
float y=0.0;
for(;;)
{
printf ("输入一个浮点数, 输入字符退出.\n");
if (scanf("%f",&y)==0) return 1;
xxx.f=(float)y;// change this for other data
itoa(xxx.i,bit,2);//整数转换为二进制字串
strrev(bit);// 翻转字串
unsigned int len=strlen(bit);
for (int jj=0;jj<(int)(32-len);jj++)
{// 补零
strcat(bit,"0");
}
strrev(bit);// 恢复
printf("%f = %32s = %08X\n",xxx.f,bit,xxx.i);
}
//
return 0;
}
??
希望对你有帮助~