Data storage in memory (original code, inverse code, complement code calculation)


foreword

Tip: This article mainly introduces the storage of data in memory.


1. Detailed introduction of data types

1. Data type

Basic data types:

char          //字符数据类型
short        //短整型
int          //整型
long        //长整型
long long   //长长整型
float       //单精度浮点数
double     //双精度浮点数

2. Basic classification of types

The types can be divided into:
(1) Integer types : short, int, long, char (characters are stored in the form of ASCII code in memory), long long.
(2) Floating point type : float, double.
(3) Structure type : array type, structure type (struct), enumeration type (enum), union type (union).
(4) Pointer type : int * p, char * a, float * c, void * a
(5) Void type : void means empty type (no type), usually applied to function return type, function parameter, pointer type )


Second, the storage of integers in memory

Integer data is stored in the form of two's complement in memory .
Reason: The sign bit and the value bit can be processed uniformly by using complement code, and at the same time, addition and subtraction can also be processed uniformly.
(1) Original code: convert the number into binary, and add a sign bit in front of the value (that is, the highest bit is the sign bit): the bit is 0 for positive numbers, and 1 for negative numbers (0 has two representations: +0 and -0), and the remaining bits represent the magnitude of the value.
(2) Inverse code: keep the sign bit of the original code unchanged, and invert the other bits in turn.
(3) Complement code: add one to the complement code to get the complement code.

For example:
insert image description here


3. Introduction and judgment of big and small endian byte order

Big end (storage) mode: means that the low bits of data are stored in the high address of memory, and the high bits of data are stored in the low address of memory; little
end (storage) mode: means that the low bits of data are stored in the low address of memory , while the high bits of the data are stored in the low address of the memory;

In the VS compilation environment, data is stored in memory in the big-endian storage mode.


4. Storage of floating-point types in memory


Floating-point types include single- precision floating-point and double -precision floating-point .
It is divided into three parts in storage:
S sign bit (Sign): 0 means positive, 1 means negative
E index bit (Exponent): used to store exponent data in scientific notation, and use shift to store
M Mantissa: mantissa part

Single precision occupies 4 bytes, a total of 32 bits. Its format is:
1-bit sign, 8-bit exponent, 23 decimal places. The effective number of digits is 7 digits (6 decimal places + decimal point).
Taking float as an example, its storage method is shown in the figure below:
insert image description here

Double precision occupies 8 bytes, a total of 64 bits in size, and its format is:
1-bit sign, 11-bit exponent, and 52-bit decimal. The effective number of digits is 16 bits (15 decimal places + decimal point),
and its storage method is shown in the figure below:
insert image description here

The more digits the decimal part occupies, the more significant digits the number has, and the higher the accuracy.
The more digits the exponent part occupies, the larger the range of values ​​that can be represented.


Summarize

That's all for this article.

Guess you like

Origin blog.csdn.net/m0_53689542/article/details/121721773