Advanced C language-data storage in memory

Advanced C language-data storage in memory

First of all, we must know that all our things are stored in memory, no matter what the data or what, in the computer, taking a 32-bit operating system as an example, a storage unit is a byte, 8-bit, 2-digit hexadecimal number, each Each storage unit has a corresponding address, which is an 8-digit hexadecimal number.
When we want to store data, for example:

int a=0x12345678//十六进制

So should it be stored at 12 34 56 78, or should it be stored at 78 56 34 12?
This involves the small-endian model . The so-called big-endian mode means that low addresses store high data bits and high addresses store low data bits; the little-endian mode is just the opposite. And our computer is stored in little-endian mode, so how do we verify it?

int a=0x12345678;
char *p=(char*)&a;
printf("%x\n",*p);

Insert picture description here
Because 78 is the low data bit, and a is 4 bytes, p is a char* type pointer, which points to char type data, and can only dereference 1 byte, so the result is 78, that is, the computer storage mode is little-endian.
Next, is the number of bytes occupied by the basic data types :

char //1字节
short//2字节
int//4字节
long//32位为4个字节,64位为8个字节
long long//8字节
float//4字节
double//8字节

For shaping data, I take the int type as an example, 4 bytes, 32 bits (the highest bit is the sign bit):

//有符号整形
int a=10;
int b=-1;

Because a is a positive number, the storage method is the corresponding binary code. If there is not enough 32 bits, add 0:
The original code corresponding to 10: 0000 0000 0000 0000 0000 0000 0000 1010
For b, b is a negative number, so pay attention to negative numbers The storage in the memory is stored in the form of complement, why? Because there is no subtraction in the computer, why do you say that? Example
1: 0000 0000 0000 0000 0000 0000 0000 0001
-1: 1000 0000 0000 0000 0000 0000 0000 0000 0001
logically add 1+(-1)=0, but After adding it down, it is actually equal to -2, but the computer is still 1-1=2. This is because the computer stores the complement of negative numbers.
How to calculate the complement? Remember: original code->inverse code->complement code
inverted code = the original code is inverted by bit except for the sign bit.
Complement code=inverted code+
1 -1 original code: 1000 0000 0000 0000 0000 0000 0000 0001
inverted code: 1111 1111 1111 1111 1111 1111 1111 1110
Complement: 1111 1111 1111 1111 1111 1111 1111 1111

//无符号整形
unsigned int a=1;

The difference between unsigned integer and signed integer is that the highest bit of unsigned integer is no longer the sign bit, but the value bit.
In addition, the most important thing to pay attention to is the floating-point type, here is the float type as an example:

float a=10.5f

Integer part corresponds to binary: 10->1010
decimal part corresponds to binary: 0.5->0
So 10.5 corresponds to binary is 1010.0=1.0100*2^3 The exponent is 3, and because the exponent may be negative, there is already a sign bit, so Uniformly stipulate that the exponent bit stores the binary number after exponent +127. 3+127=130->1000 0010

Sign bit (1 bit) Exponent bit (8 bits)
0 1000 0010
Mantissa (23 digits) (not enough to add zeros afterwards)
0100 0000 0000 0000 0000 000

So the final result is:
0100 0001 0010 0000 0000 0000 0000 0000
so floating-point data and integer data storage are two different things, we must pay attention! ! ! ! ! !

Guess you like

Origin blog.csdn.net/qq_45841205/article/details/109860800