How C + + + + floating-point numbers are stored in memory

Source: Internet
Author: User

Any data in memory is stored in binary form, such as a short data 1156, and a binary representation of 00000100 10000100. In the Intel CPU architecture of the system, the storage method is 10000100 (low address unit) 00000100 (High address unit), because the Intel CPU architecture is a small terminal mode. But how are floating-point numbers stored in memory? All of the C + + compilers currently use the standard floating-point format developed by IEEE, the binary scientific notation.

In binary scientific notation, S=M*2^N consists of three main parts: the sign bit + order code (N) + mantissa (M). For the float type data, the second binary has 32 bits, wherein the sign bit 1 bits, the order 8 bits, the mantissa 23 bits; for double type data, the second binary is 64 bits, the sign bit 1 bits, the order code 11 bits, the Mantissa 52 bits.

31 30-23 22-0

Float sign bit order mantissa

63 62-52 51-0

Double sign bit order Mantissa

Sign bit: 0 means positive, 1 means negative

Order code: Here the order code to use the shift code, for the float type data, its specified offset is 127, the order has a positive negative, for 8-bit binary, then its representation range is -128-127,double type 1023, which represents a range of-1024-1023. For example, for float type data, if the real value of the order code is 2, then add 127 after 129, and its order representation is 10000010

Mantissa: A valid digit bit, that is, part bits (bits after the decimal point), because the integer portion of the specified m is constant 1, so this 1 is not stored.

The following examples illustrate:

Float data 125.5 converted to standard floating-point format

The 1252 binary representation is 1111101, the fractional part is represented as binary 1, the 125.5 binary is represented as 1111101.1, because the integer portion of the mantissa is constant 1, it is 1.1111011*2^6, the order is 6, plus 127 is 133, is represented as 10000101, and for the mantissa, the integer Part 1 is removed, 1111011, after which 0 of the number of digits to 23 bits, then 11110110000000000000000

Then the binary representation is

0 10000101 11110110000000000000000, the memory is stored in the following way:

00000000 Low Address

00000000

11111011

01000010 High Address

How C + + + + floating-point numbers are stored in memory

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.