*****float type occupies 4 bytes in memory, that is, 32 bits. Similarly, int or long type also occupies 4 bytes. After decomposing the respective 32 bits bit by bit, It is found that the specific meanings they represent are quite different.
****First decompose the 32-bit integer. For unsigned int, the decomposition is as follows: from right to left, record the value of the i-th bit as bi (0 or 1), Then the decimal value represented by this bit is: bi * 2^i, i=0,2,...31. The integer 4-byte (32-bit) binary number converted into decimal number is: b31 * 2^31 + ... + bi * 2^i + ... + b1 * 2 + b0 * 1 - recorded as " formula@".
****For signed int, the highest bit (bit 31) represents the sign, 0 is positive and 1 is negative. The remaining 31 bits must be subtracted by 1 and then inverted, recorded as "Operation #", and then press the above "Formula @" to get the absolute value of this integer.
****The above is an introduction, and the following float decomposition must also be based on this. Remember the two agreed meanings of "operation #" and "formula @". At the same time, I would like to correct the questioner's statement that "the range that can be expressed should be 0-2^32". The accurate numerical range of integers is: signed ( (-2^31) —— (2^31-1)), Unsigned (0 - (2^32-1)). Let's lead to the key question. What do the 32 bits of float mean in memory?
***** represents all 32 bits as b31-b0 (from high to low). b31 is also the sign bit, and its meaning is 0 positive and 1 negative. The total ***8 bits of b30-b23 are a signed int. Use "operation #" and "formula @" to calculate a value E (the decimal range is -128 —— 127), and then pass it through The exponential operation yields 2^E, and the decimal range is: 1.0 * 10^(-38) - 1.7 * 10^38. The remaining 23 digits b22-b0 represent decimal values. The decimal value represented by b22 is: b22 * 2^(-1), which is b22 * 0.5. The decimal value represented by b21 is: b22 * 2^(-2), which is b22 * 0.25, and so on, so the decimal range is 0 - 1 (actually 0.0000...1 to 0.9999...).
****From the above decomposition, we can see that the 32 bits of float have their own meanings and can be divided into 3 blocks, symbol block S (b31), exponent block E (b30-b23), The decimal block D(b22-b0), but how to assemble it into an actual small tree? The specific algorithm is:
(-1)^S * (1.0 + D) * 2^E, and the final value range is -3.4*10^-38 - 3.4*10^38.
*****Finally, a reminder, all floating point numbers go through the corresponding hardware to perform the above "operation #" and "formula @" when entering and exiting the memory. Programmers do not need to delve into the details. If they are out of curiosity, it is worth understanding and supporting.