The analysis of the storage mode of the floating point number in memory and the example _c language

Source: Internet
Author: User

The analysis of the storage mode of the floating point number of C + + in memory

Any data is stored in binary form in memory, such as a short data 1156, and a binary representation of 00000100 10000100. In the Intel CPU architecture system, the storage mode is 10000100 (Low address unit) 00000100 (high address unit) because the Intel CPU architecture is a small-end mode. But how do you store floating-point numbers in memory? At present, all C + + compilers use the standard floating-point format developed by IEEE, that is, binary scientific notation.

In binary scientific notation, s=m*2^n consists mainly of three parts: sign bit + order (N) + mantissa (M). For float data, the binary system has 32 digits, in which the sign bit 1 bits, the order code 8 bits, the mantissa 23 digits, for double data, the binary system is 64, the sign bit 1, the order Code 11, the Mantissa 52 bit.

31 30-23 22-0

Float sign bit order mantissa

63 62-52 51-0

Double sign bit order Mantissa

Sign bit: 0 indicates positive, 1 indicates negative

Order code: Here the order code using the shift code, for float-type data its specified bias is 127, the order has a negative, for the 8-bit binary, its range of representations for the -128-127,double type of 1023, the range of the expression is 1024-1023. For example, for float-type data, if the real value of the order is 2, then add 127 to 129, and its order form is 10000010.

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

The following examples illustrate:

Float data 125.5 converted to standard floating-point format

1252 binary representation is 1111101, the decimal part is represented as binary 1, then 125.5 binary is 1111101.1, because the integer part of the mantissa is 1, the order is 1.1111011*2^6, the rank code is 6, plus 127 is 133, is expressed as 10000101, and for the mantissa, the integer part is 1 removed, 1111011, followed by 0 so that its digits are 23 digits, then 11110110000000000000000

The binary representation is

0 10000101 11110110000000000000000 is stored in memory in the form of:

00000000 Low Address

00000000

11111011

01000010 High Address

and conversely, to calculate floating-point numbers in binary form, like 0 10000101 11110110000000000000000.

A positive number because the symbol is 0. The order is 133-127=6 and the mantissa is 11110110000000000000000, the true mantissa is 1.1111011. So its size is

1.1111011*2^6, move the decimal point to the right 6 digits, get 1111101.1, and 1111101 of the decimal 125,0.1 decimal is 1*2^ (-1) = 0.5, so its size is 125.5.

Similarly, if you convert float data 0.5 to binary form

0.5 binary form is 0.1, because the specified positive part must be 1, the decimal point to the right 1 digits, then 1.0*2^ (-1), its order is -1+127=126, expressed as 01111110, and the Mantissa 1.0 to remove the whole number of parts is divided into 0, complete 0 to 23 digits 00000000000000000000000, then the binary representation form is

0 01111110 00000000000000000000000

The maximum representation range of float-type data is 1.11111111111111111111111*2^127=3.4*10^38 from the above analysis

Similar to double data, except that the order is 11 digits, the offset is 1023, and the mantissa is 52 bits.

Test Program:


* * Test floating-point data in memory storage mode 2011.10.2*/ 

#include <iostream>
using namespace std;

int main (int argc, char *argv[])
{
  float a=125.5;
  Char *p= (char *) &a;
  printf ("%d\n", *p);
  printf ("%d\n", * (p+1));
  printf ("%d\n", * (p+2));
  printf ("%d\n", * (p+3));
  return 0;
}

The output results are:

0

0

-5

66

It is already known on the above that float 125.5 is stored in memory in the form of:

00000000 Low Address

00000000

11111011

01000010 High Address

So for the cells that P and p+1 point to, where the binary number is stored, the decimal integer represented is 0;

For p+2-pointed units, because they are char pointers, are signed data types, so 11111011, the symbol bit is 1, is negative, because in memory binary is in the complement, so its true value is-5.

For a cell that p+3 points to, 01000010 is a positive number, and its size is 66. The output of the above program verifies its correctness.

Thank you for reading, I hope to help you, thank you for your support for this site!

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.