How is data stored in memory?

Who can slash immediately, but I am the general Fei Niu!

———————————————————————————————————————————————————————

The topic of this section: #How is data stored in memory?

———————————————————————————————————————————————————————

What types of data are there?

.
.

first: the basic built-in type

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

So, does the C language have a string type?

answer is yesno

but we can

const char *str = "Hello world";
char str[] = "Hello world";

To represent a string like this

So, what's the point of types? There are two meanings:

One: it determines the size of the memory space we open up when we define variables
; two: it determines how we look at the perspective of memory space

.
.

second: construction type

> 数组类型
> 结构体类型 struct
> 枚举类型 enum
> 联合类型 union

.
.

third: pointer type

int * p;
char *q;
float * s;
void *f;
double *k;

.
.

forth: empty type

>void表示空类型

>通常应用于函数的返回类型,函数的参数,指针类型等

———————————————————————————————————————————————————————

Integer storage in memory

.
.

Original code, inverse code, complement code

1. The sign bit representation method is the same

a. The three representation methods all have a sign bit and a value bit
b. The sign bit means the same, 0 means positive, 1 means negative

2. The numerical representation method is different
original code

It is OK to directly translate binary into binary in the form of positive and negative numbers.

inverse code

The symbol bit of the original code remains unchanged, and the other bits are reversed bit by bit, and it is OK.

Complement

Add 1 to the inverse code to get the complement code

The original code, complement code and complement code of the integer are the same

For integer data, the data is stored in the memory in the form of two's complement

Let's take a look at how integer data is stored in memory

insert image description here

It can be seen that the integer data is stored in the form of complement code in the memory, but there seems to be a problem here. Why is the order a bit wrong?

OK, I have a showdown, and there are big and small endian issues involved here

.
.

Big and small endian problem

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

The little endian (storage) mode means that the low bits of the data are stored in the low addresses of the memory, while the high bits of the data are stored in the high addresses of the memory.

> Write a simple program to verify big and small endian

    int a = 20;
	printf("%d\n", *((char*)&a));

———————————————————————————————————————————————————————

Storage of floating point types in memory

Any binary number can be expressed as a floating point number in the following form: (-1)^S * M *2^E

a. (-1)^S is the sign bit—> S is 0, it means a positive number, S is 1, it means a negative number b. M means a significant number—> greater than or equal to 1, less than
2
c. 2^E means an exponent bit

Single-precision floating-point number storage model

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 number M

Double-precision floating-point number storage model

For a 64-bit floating-point number, the highest bit is the sign bit S, the next 11 bits are the exponent E, and the remaining 52 bits are the significand M.

There are some special provisions for the significant figure M and the exponent E

Part M

Since 1≤M<2, M can be written as 1.xxxxxx, where xxxxxx represents the decimal part. When saving M inside the computer, the default first digit is 1, so it can be omitted. Originally, 23 digits of M can be saved, but now it can be saved 24 bits

E part (E is an unsigned integer)
deposit status

We know that E in scientific notation can have negative numbers, so when saving, add amiddle number, for 8-bit E is 127, for 11-bit E is 1023, for example, E of 2^5 is 5, so when saving it as a 32-bit floating point number, you must save 5+127 = 132, and here 132 should be written as The binary form of 8-bit E

Take out the situation(three conditions)
1. E is not all 0 or not all 1

Subtract 127 from the value of E to get the real value, and then add the first digit of 1 in front of M,
such as 0.5, which is 0.1 in binary. Since the positive part must be 1, move the decimal point to the right to get 1.0*2 ^(-1), the order code is -1+127 = 126, and the binary value is 01111110. When saving M inside the computer, the default first digit is 1, so it can be omitted, and the solution is: 0 01111110
00000000000000000000000

E is all 0

At this time, the real value is 1-127 (or 1-1023), and the first digit of 1 is no longer added in front of M, and the whole number is restored to a decimal of 0.xxxxxx, which means that it is close to positive or negative 0, and close to 0. very small number

E is all 1

At this time, if all valid digits are 0, it means positive and negative infinity

.
.

Good times are always short, see you next time!

PS: If my blog post is lucky enough to be seen by some friends, and if I think my writing is okay, you can subscribe to support it, manually follow it without getting lost, and go to the high speed with Feiniu, haha!

Guess you like

Origin blog.csdn.net/Flying_Cow_Z/article/details/106660548