Home > Back-end >  IEEE754 decimal number
IEEE754 decimal number

Time:10-11

The code is as follows:
#include
#include
# include "math. H"
#include
#include

using namespace std;

Void fun (bool m)/print/
{
Cout}
Vector//get_hex_2b (unsigned int a) hexadecimal conversion into binary
{
Vector X;
for (int i=0; i<32. I++)
{
If (((a) (int) & amp; (0 x80000000 & gt;> I)))
{
X.p ush_back (1);
}
The else
{
X.p ush_back (0);
}
}
///////////////////////////////////print the hex binary code
Cout<& lt;" Binary code: "& lt; For_each (x.b do v.begin (), x.e nd (), fun);
Cout/////////////////////////////////////
return x;
}
Int get_hex_jiema (vector A)//parameter for short floating-point binary code
{
//the original 1-8 bits of binary code to frameshift container
Vector Yima (a. do v.begin () + 1, a. do v.begin () + 9);

Vector : : iterator ite=yima. The begin ();//set container iterator
Unsigned int sum=0;
//calculation decimal number of binary code
for(int i=0; i<8; I++)
{
Sum=sum + (* ite) * pow (2, 7 - I);
Ite++;
}
Int jiema=sum - 127;
Return jiema;
}
Float get_10dec (vector A)//parameter for short floating-point binary code
{
//save the integer part of binary, one of the initial value 1, size for the tail implied a
Vector Zhengshu (1, 1);

Vector Xiaoshu.//save the decimal part of a binary

//original binary code to 9 - the tail to tail yards container
Vector Weima (a. do v.begin () + 9, a.e nd ());

//according to size of exponent, shift to the left tail code, get the integer binary code
Zhengshu. Insert (zhengshu. End (), weima. The begin (), weima. The begin () + get_hex_jiema (a));

//the rest of the tail code for decimal binary code
Xiaoshu. Insert (xiaoshu. End (), weima. The begin () + get_hex_jiema (a), weima. The end ());
//////////////////////////////////////////

//print an integer and fractional part of binary code
Cout<& lt;" The integer part, "& lt; For_each (zhengshu. The begin (), zhengshu. The end (), fun);
CoutCout<& lt;" The decimal part: "& lt; For_each (xiaoshu. The begin (), xiaoshu. The end (), fun);
Cout/////////////////////////////////////////////
Float zheng_shu=0;
Float xiao_shu=0;
Float sum=0;

//calculate the decimal integer sizes
For (int I=zhengshu. The size (); i> 0; I -)
{
Zheng_shu=zheng_shu + zhengshu pow [I - 1) * (2, zhengshu. The size () - I);
}

//calculate the decimal decimal size
for(int i=0; i{
Xiao_shu=xiao_shu + xiaoshu, [I] * pow (2 - (I + 1));
}
Sum=zheng_shu + xiao_shu;

//determine the sign bit
If (a [0]==1)
Sum=0 - sum;

return sum;
}

Int main ()
{
Unsigned int a;
Float sum=0;
Int e=0;
Cout<& lt;" Please input is converted into decimal IEEE754 standard short floating-point hexadecimal form: "& lt; Cin> Hex> a;//hexadecimal input
E=get_hex_jiema (get_hex_2b (a));
Cout<& lt;" The size of the order code: "& lt; Sum=get_10dec (get_hex_2b (a));
Cout<& lt;" Converted to a decimal number is: "& lt; CoutCoutsystem("pause");
return 0;
}


Terminal error:
Please input is converted into decimal IEEE754 standard short floating-point hexadecimal form:
Be55b000
Binary code:
10111110010101011011000000000000
The size of the order code: - 3
Binary code:
10111110010101011011000000000000
The terminate called after throwing an instance of 'STD: : length_error'
What () : vector : : _M_insert_range
Have been abandoned (core dump)
  • Related