C language storage

 

 

Preface

  • Data type detailed introduction
  • Storage of shaping in memory: original code, inverse code, complement
  • Introduction and judgment of big and small bytecode
  • Floating point type is stored in memory and analyzed

 

One, data type introduction

char //Character data type

short //short integer

int //Shaping

long //long integer

long long //Longer shaping

float //Single precision floating point number

double //Double precision floating point number

Basic classification of types:

Integer family; floating-point number family; structure type; pointer type; null type

Here we will subdivide the structure types: array type; structure type struct; enumeration type enum; union type union;

 

Second, the storage of shaping in memory

The original code directly translates the binary into binary in the form of positive and negative numbers.

The inverse code keeps the sign bit of the original code unchanged, and the other bits can be obtained by inverting bit by bit.

Complement code +1 is complement code.

The original, inverse and complement of positive numbers are all the same. For plastic shaping: the data storage memory actually stores the complement code.

Note: In the computer system, the value is always expressed and stored in one's complement code. With the use of one's complement code, the sign bit and the value field can be processed uniformly; addition and subtraction can also be processed uniformly (CPU only has an adder).

When it comes to storage, you have to mention big-endian and little-endian

Big-endian (storage) mode means that the low bit of data is stored in the high address of the memory, and the high bit of data is stored in the low address of the memory;

Little-endian (storage) mode means that the low-order bits of the data are stored in the low addresses of the memory, and the high-order bits of the data are stored in the high addresses of the memory.

Three, floating-point type is stored in memory

According to the international standard IEEE (Institute of Electrical and Electronic Engineering) 754, any binary floating point number V can be expressed in the following form:

(-1)^S * M * 2^E

(-1)^s represents the sign bit, when s=0, V is a positive number; when s=1, V is a negative number.

M represents a significant number, greater than or equal to 1, and less than 2.

2^E represents the exponent bit

For example: 5.0 in decimal, 101.0 in binary, equivalent to 1.01×2^2. Then, according to the format of V above, we can get s=0, M=1.01, E=2. -5.0 in decimal, -101.0 in binary, equivalent to -1.01×2^2. Then, s=1, M=1.01, and E=2. IEEE 754 stipulates: For 32-bit floating-point numbers, the highest 1 bit is the sign bit s, the next 8 bits are the exponent E, and the remaining 23 bits are the significant digits M.

IEEE 754 has some special regulations on significant figures M and exponent E. As mentioned earlier, 1≤M<2, that is to say, M can be written in the form of 1.xxxxxx, where xxxxxx represents the decimal part. IEEE 754 stipulates that when saving M in the computer, the first digit of this number is always 1 by default, so it can be discarded and only the following xxxxxx part is saved. For example, when saving 1.01, only 01 is saved, and when it is read, the first 1 is added. The purpose of this is to save 1 significant figure. Take a 32-bit floating-point number as an example. There are only 23 bits left for M. After the 1 in the first bit is rounded down, it is equal to 24 significant digits.

As for the index E, the situation is more complicated.

First, E is an unsigned int. This means that if E is 8 bits, its value range is 0~255; if E is 11 bits, its value range is 0~2047. However, we know that E in scientific notation can appear negative numbers, so IEEE 754 stipulates that the true value of E must be added with an intermediate number when stored in memory. For 8-digit E, the intermediate number is 127; For an 11-digit E, the middle number is 1023. For example, the E of 2^10 is 10, so when saving as a 32-bit floating point number, it must be saved as 10+127=137, which is 10001001.

E is not all 0 or not all 1 At this time, the floating-point number is expressed by the following rule, that is, the calculated value of the exponent E is subtracted from 127 (or 1023) to obtain the true value, and then the effective number M is added before the first digit 1.

When E is all 0, E=1-127 (or 1-1023) is the true value, which represents a very small number close to 0.

E is all 1 at this time, it means infinity (positive and negative depends on the sign bit s)

 

#include<stdio.h>
int main()
{
    int n = 9;
    float *pFloat = (float *)&n;
    printf("n的值为:%d\n", n);   //9

    //0 000 0000 0 000 0000 0000 0000 0000 1001
    //s e m
    //0 -126 0.000 0000 0000 0000 0000 1001
    printf("The value of *pFloat is: %f\n", *pFloat);

    *pFloat = 9.0;  //1001.0 => (-1)^0 * 1.001 * 2^3

    //0 100 0001 0 001 0000 0000 0000 0000 0000
    //0100 0001 0001 0000 0000 0000 0000 0000
    // 4 1 1 0 0 0 0 0
    printf("num value: %d\n", n); / /
    printf("*pFloat value is: %f\n", *pFloat);
    return 0;
}

 

Why is 0x00000009 reduced to a floating point number and becomes 0.000000? First, split 0x00000009 to get the first sign bit s=0, the next 8 bits of exponent E=00000000, and the last 23 bits of significant digits M=000 0000 0000 0000 0000 1001.

Since the index E is all 0, it conforms to the second situation in the previous section. Therefore, the floating-point number V is written as: V=(-1)^0 × 0.00000000000000000001001×2^(-126)=1.001×2^(-146) Obviously, V is a small positive number close to 0, so Expressed as a decimal fraction is 0.000000.

 

the second part. How does the floating-point number 9.0 use binary representation? How much is it reduced to decimal? First, the floating point number 9.0 is equal to 1001.0 in binary, which is 1.001×2^3.

9.0 -> 1001.0 ->(-1)^01.0012^3 -> s=0, M=1.001,E=3+127=130

The sign bit of the first digit s=0, the effective digit M is equal to 001 and 20 0s are added to make up 23 digits, and the exponent E is equal to 3+127=130, which is 10000010. So, written in binary form, it should be s+E+M, that is

0 10000010 001 0000 0000 0000 0000 0000

This 32-bit binary number, reduced to decimal, is exactly 1091567616.

 

Guess you like

Origin blog.csdn.net/weixin_45070922/article/details/110094460