Memory layout for C + + float

Source: Internet
Author: User
Tags binary to decimal

IEEE754 Standard, floating-point memory layout

The following is only described by float (4 bytes in memory, 32bits), double (8 bytes, 64bits) in the same vein, only slightly different.

The memory distribution of float

IEEE754 Specifies that 32bit float is distributed in memory:

sign Bit (S) Order Code (E) mantissa (M)
1 8 23

integers, floating-point numbers in memory are in binary form (complement), while floating-point numbers are binary scientific notation stored in memory. Can be expressed as: $ s 1.m^e$

    • The sign bit occupies a bit, which represents a positive number when s = 0, and a negative number when s = 1.
    • The order E is the number of its exponent plus an offset (127), which occupies a total of 8 bytes, the range of 8-bit unsigned number is 0~255, considering that an offset has been added, the actual expression of the range is -128~127 (this range refers to the range of binary notation).
    • The mantissa m occupies 23 bytes, so any floating-point number (except 0) implies a 1 because it is the scientific notation used in the binary, that is, its mantissa should actually be preceded by another 1, i.e.: 1.m , so that the binary precision of the floating-point number is 24 bits, and $2^{24} = 16777216$, so its decimal precision is 7 valid digits.
Compute instances

1, decimal and binary conversion
binary to decimal is relatively simple, is the corresponding bit multiplied by the corresponding power of 2, such as: 101.1011, the conversion process is:
$$1*2^2 + 0 * 2^1 + 1 * 2^0 + 1 * 2^{-1} + 0 * 2^{ -2} + 1 * 2^{-3} + 1 * 2^{-4}$$
Decimal integer to binary simpler, one is 8421来, one is in addition to 2 in reverse row, no longer explained.
Decimal Fractional to binary, that is, always multiply by 2, if greater than 1, then set 1, if less than 1, then 0, always multiply by 2, until 0 or the specified number of digits is reached.
such as 0.125:
$$ 0.25 * 2 = 0.5---0 \ 0.5 * 2 = 1---1$$
then the second binary is: 0.01.
Another example: 0.632:
$$ 0.632 * 2 = 1.264---1 \ 0.264 * 2 = 0.528---0 \ 0.528 * 2 = 1.056---1 \ 0.056 * 2 = 0.112---0 \ ... $$
The second binary is: 0.1010 .....
2, calculating the memory layout of Float
2.1 Consider float FA = 4.25 , then: int ia = * (int*) &fa How much is it?
We know that 4.25 = 100.01 , the exponent is represented as: $1.0001*2^2$, then its sign bit s = 0, the order e = 2 + 127 = 0x81, the tail part of the binary is expressed as: 1000 1000 0000 0000 0000 0000 , a total of 24 bits, the highest bit of the mantissa 1 is removed, and then the order E, sign bit s combined:

sign Bit (S) Order Code (E) mantissa (M)
0 1000 0001 000 1000 0000 0000 0000 0000

Together It is:
0100 0000 1000 1000 0000 0000 0000 0000, namely: ia = 0x40880000 .
2.2 Think again float fa = -0.0125 , that to: c++ int ia = *(int*)&fa how much is it?
-0.0125 = -0.00000011001100110011001100..., converted to an exponent of: $-1.10011001100110011001100...*2^{-7}$, then its sign bit s = 1, the order e = 7 + 127 = 0x78, the tail part of the binary is expressed as:, the 1100 1100 1100 1100 1100 1101 following number of bits is rounded truncated, Since the last one is 1, move forward 1, also retain 24 bits, remove the highest bit of 1, and then combine with the order code and the symbol bit:

sign Bit (S) Order Code (E) mantissa (M)
1 0111 1000 100 1100 1100 1100 1100 1101

All of them are:
1011 1100 0100 1100 1100 1100 1100 1101, namely: ia = 0xbc4ccccd .

If you convert the IA to FA, the conversion process is reversed.



From for notes (Wiz)

Memory layout for C + + float

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.