Ling Yu posted the [IEEE floating point representation] In www.360doc.com. After careful research, he finally got a little bit of experience.
Float to hexadecimal:
According to the IEEE Standard, the hexadecimal floating point is divided into 32-bit and 64-bit. The parameters are as follows:
| |
Symbol bit |
Exponential bit |
Exponential offset |
Tail Digit |
| 32-bit |
1 [31] |
8 [23-30] |
23 [0-22] |
127 |
| 64-bit |
1 [63] |
11 [52-62] |
52 [0-51] |
1023 |
Float
A total of 32 bits, 4 bytes
From the highest to the lowest Bit are 31st, 30, 29 ,...... 0 bits
31 is the symbol bit. 1 indicates that the number is negative, and 0 indicates that the number is negative.
30-23 digits. A total of 8 digits are exponential digits.
22-0 digits. A total of 23 digits are the ending digits.
Now let's use the IEEE floating point number representation to step-by-step convert the float floating point number 123456.0f to the hexadecimal code.
When processing this floating point number without decimal digits, the integer is directly converted into a binary representation: 1 11100010 01000000.
It can also be expressed as 11110001001000000.0.
Then move the decimal point to the left, and move it to only one place away from the highest digit, that is,. 11100010010000000 of the highest digit.
A total of 16 digits are moved. In the bucket operation, each decimal point shifted to the left is equal to the exponent + 1 in the scientific calculation method at the bottom of 2, so the original number is equal to the following: 1.11100010010000000*(2 ^ 16 ).
Well, now we have both the ending number and the index. Obviously, the highest digit is always 1, because you can't say that you have bought 16 eggs as you have bought 0016 eggs, right? So do we need to keep this 1? Okay, let's delete it.
In this way, the binary value of the ending number is changed to: 11100010010000000, and then 0 is added after the ending number until 23 bits are supplemented: 11100010010000000000000.
Let's look at the index again. A total of eight digits can indicate an unsigned integer in the range of 0-255, or a signed integer in the range of-128-127.
However, because the index can be negative, 127 is added to convert the decimal integer into binary. Here, we add 127 to 16 and then 143, the value is 10001111 in binary format.
The number of 12345.0f is positive, so the symbol bit is 0, so we can combine it according to the preceding format:
0 10001111 11100010010000000000000
01000111 11110001 00100000 00000000
Convert it to hexadecimal: 47 F1 20 00, and finally turn it over to: 00 20 F1 47.
========================================================== ============================
According to the IEEE floating point number representation, the float floating point number 123.456f is converted into a hexadecimal code.
Convert integer to binary: 100100011.
Processing of decimal places is more troublesome.
Let's take a look at the procedure: 1/2 ^ 1 (for convenience, the following uses only the 2 Index to represent the bit ),
1st bits: 0.456 is 0 if it is less than 0.5 of the bits;
2 bits, 0.456 is greater than the rank value 0.25, this bits is 1, and 0.45 minus 0.25 is obtained into the next bits;
3 bits, 0.206 is greater than the rank value 0.125, this bits is 1, and 0.206 minus 0.125 is obtained into the next bits;
4 digits, 0.081 greater than 0.0625, is 1, and 0.081 minus 0.0625 is entered into the next digit;
The value of 5-digit 0.0185 is less than 0.03125, and the value is 0 ......
======================================
// Obtain the binary value of the floating point ending number
double d = 0.0d; double d1 =0.456; StringBuilder sb = new StringBuilder(); for (int i = 1; i < 19; i++) { d = Math.pow(2,- i); if(d1<d){ sb.append(0); }else { sb.append(1); d1 = d1 - d; } } System.out.println(sb.toString());
========================================
The problem arises, even if it exceeds the maximum length of 23 characters! This is the famous floating point Precision problem. However, I am not going to talk about "numerical calculation" here. I will use various methods to improve the accuracy of the computation, because it is too complicated. I am afraid I will not be able to figure it out in the last year. Here I will only clarify the floating point representation to achieve the goal.
In the end, I can't find it all the time. I can just add the preceding integer to the 24-bit formula: 1111011.01110100101111001. A bc asked: "isn't it a 23-bit number ?" Me: "Well, didn't you say you want to remove the first 1? Of course we need to add one !"
Now we start to move the decimal point to the left. The big guy and I move the decimal point together: "1, 2, 3 ......" Okay, I moved 6 places in total, and 6 plus 127 yuan (How do I teach primary school students? Haha ~), The binary value is 10000101, And the symbol bit is ...... ......
If you don't talk about it, the more you say it, let's take a look:
0 10000101 11101101110100101111001
42 F6 E9 79
32-bit floating point number (123.456) in the memory if the storage is 79 E9 F6 42.
Then, first order it to 42 F6 E9 79 to bring it to the top;
Convert it to binary: 0 10000101 11101101110100101111001
The highest bit is the sign bit 0, indicating a positive value;
The next 8 digits are exponential digits.
Convert to decimal and then subtract 127. Result 6;
The tail part is shifted to 6 digits 111011.01110100101111001;
Add 1 at the beginning to 1111011.01110100101111001;
Convert the integer to 1111011 in decimal format;
Decimal part 01110100101111001;
0 corresponds to the-1 power of 2, and the next 1 corresponds to the-2 power of 2:
0*2 (-1) + 1*2 (-2) + 1*2 (-3) + 1*2 (-4) + 0*2 (-5) + 1*2 (-6 )...... ≈ 0.456
The sum is close to 0. 456
Result 123.456
========================================================== ============================
Java program:
System. out. println ("Convert 123.456f floating point to hexadecimal:"); system. out. println ("decimal digit:" + float. floattointbits (123.456f); system. out. println ("decimal to binary:" + integer. tobinarystring (float. floattointbits (123.456f); string = integer. tohexstring (float. floattointbits (123.456f )). touppercase (); system. out. println ("hexadecimal:" + String); system. out. println ("hexadecimal to 10th:" + integer. parseint (string, 16); system. out. println ("decimal floating point:" + float. intbitstofloat (integer. parseint (string, 16 )));
========================================================== ======================================