[C language knowledge] Storage of data in memory

In-depth analysis of data storage in memory

1. Introduction to data types

//整型家族
char        //字符,占1字节
short       //短整型,占2字节
int         //整型,占4字节
long        //长整形,占4/8字节
long long   //更长的整型,占8字节

//浮点数家族
float       //单精度浮点数,占4字节
double      //双精度浮点数,占8字节

In addition to the above two data types, there are also the following three data types:

  • Constructed type (that is, custom type)
    • array type
    • Structure type struct
    • Enumeration type enum
    • Union type union
  • pointer type
    • int*
    • char*
    • float*
    • void*
  • empty type
    • void represents a null type, often used for return types, function parameters, pointers (null pointers)

2. Storage of integers in memory

2.1 Original code, inverse code, complement code

The original code is the 32-bit binary representation of this integer. The first bit, which is the leftmost number, is the sign bit. If the sign bit is 1, it means a negative number. If the sign bit is 0, it means a positive number. The rest 31 bits are used to represent numbers.
The complement and complement of the original code of a positive number are consistent and do not require additional calculations and conversions, but the complement and complement of a negative number require calculation .

Calculation method:

One's complement code : The sign bit of the original code remains unchanged, and the remaining digits are inverted bit by bit.
Example: The original code of -15:
10000000000000000000000000011111
The sign bit remains unchanged, and the remaining digits are inverted bit by bit:
11111111111111111111111111100000

Complement code : the complement code + 1 is enough.
For example: the complement code of -15:
111111111111111111111111111100001
Note: When the complement code is known , if you want to find the original code , you can either calculate it in reverse order, subtract one and negate it bitwise, or you can directly Inverting and adding one is the same as the calculation method of converting the original code to the complement code. These two numbers can be magically converted to each other through this calculation method.

Reminder again: the original and complement codes of positive numbers are the same, and the sizes are all original codes. No additional calculations and conversions are required.

2.2 Introduction to big and small endian

In a big-endian machine, when storing data, the high bits will be stored in the low address, and the high bits will be stored in the high address. As shown in the figure:
0x11223344 is stored in the memory.
image.png

In a little-endian machine, on the contrary, the low bits are stored in the low address and the high bits are stored in the high address. As shown in the figure:
0x11223344 is stored in the memory.
image.png

Note: 11 is a high-digit number , such as 12345 in decimal, tens, hundreds, millions, and 10,000 is a high-digit number.

3. Storage of floating point numbers in memory

Floating point numbers, meaning decimals, in scientific notation, because they can be multiplied by any number of powers of 10, so the decimal point can be regarded as floating, so it is called a floating point number.

3.1 Floating point number storage rules:

According to the IEEE754 standard, a floating point number can be represented by SEM:
S represents the sign bit
E represents the exponent
M represents the specific valid value of the number

Among them, in single-precision floating point numbers, there are a total of 32 bits. The distribution of SEM is as follows:
image.png
you can see that S occupies 1 bit, E occupies 8 bits, and M occupies 23 bits.

In double-precision floating point numbers, there are 64 bits in total. The distribution of SEM is that S occupies 1 bit, E occupies 11 bits, and M occupies 52 bits. I will not go into details here.

3.2 Special provisions for M and E:

For M, since binary decimals are stored in the memory, 1<=M<2, that is to say, M can be written as 1.xxxxxxxxx, that is, the first bit is always 1.
Therefore, during actual storage, the first digit 1 will not be stored in the memory, but 1 will be added directly to the first digit when reading.
In this way, one bit can be saved to store data. For 32-bit floating point numbers, M, which could only store 23 significant digits, can now store 24 bits.

For E, because the exponent in scientific notation has the possibility of being negative, in order to represent a negative number, E will add an intermediate number when storing, and subtract the intermediate number when reading. In a 32-bit floating point number, the space for 8 bits is 255, and the intermediate number is 127. In a 64-bit floating point number, the space for 11 bits is 2047, and the intermediate number is 1023.

To sum up, when reading a floating point number, the middle number (127 or 1023) will be subtracted from the value of E first, so that E changes back to the real value, and then 1 is added to the front of M. This allows you to read floating point numbers.

write at the end

If this article is helpful to you, can you give me a small like ❤~ Your support is my biggest motivation.

The blogger is a novice with little talent and knowledge, so it is inevitable that there will be some mistakes. Everyone is welcome to discuss and raise questions, and the blogger will correct them as soon as possible.

Thanks for watching hehehe! (〃∀〃)ゞ

Guess you like

Origin blog.csdn.net/weixin_70218204/article/details/132365255