data storage

1. Shape storage in memory

Original code, inverse code, complement code

Integers are stored in memory in two's complement form.
For signed integers, the highest bit of the binary represents the sign bit, 0 represents a positive number, and 1 represents
the conversion between the original code, the inverse code, and the complement code of the negative number.
positive integer:
The original, the reverse, and the complement are the same.
negative integer:
The sign bit of the original code remains unchanged, and the other bits are inverted to get the inverse code, and the
inverse code is added by 1 to become the complement code.

2. Type conversion

1. Implicit type conversion

Anything lower than intthe type must be converted to the type when the operation is performed int. This is called integer promotion
. Types that need to be converted
char
unsigned char
signed char
short
unsigned short

In the C language standard, there is no provision forcharwhether it is a signed or unsigned integer depends entirely on your compiler
whencharWhen the signed integer is, the range of the value it represents is ==-128~127==
When it is an unsigned integer, the range of the value it represents is0~255
This is the signcharA circle formed between -128~127, when 127+1 becomes -128

shortis a signed type, it andunsigned shortLook at the code below to
help you understand

int main()
{
    
    
	char a = 0xb2;
	整型提升为0xffffffb2
	short b = 0xb200;
	整型提升为0xffffb200
	int c = 0xb2000000;
	不需要提升
	if (a == 0xb2)
		printf("a\n");
	if (b == 0xb200)
		printf("b\n");
	if (c == 0xb2000000)
		printf("c\n");
	return 0;
}

insert image description here

2. Arithmetic conversion

When several numbers are operated together, if the types of these numbers are different, they need to be changed to the same type for operation, which is the ordinary arithmetic conversion .
These types are sorted from high to low
long double
double
float
unsigned long int
long int
unsigned int
int

Arithmetic conversion should be meaningful. When high precision is converted to low precision, some data will be missing.

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

Note: Stored in bytes
Big-endian storage : the low-order bits of the data exist at the addresshighwhere the upper bits of the data are stored at the addressLowThe place.
Little-endian byte order storage : the low-order bits of the data exist at the addressLowwhere the upper bits of the data are stored at the addresshighThe place.
insert image description here
Please program your implementation: determine which storage method your machine is

int panduan()
{
    
    
    int i = 1;
    return *(char*)&i;
}
int main()
{
    
    
    int ret=panduan();
    if (ret == 1)
        printf("小端\n");
    else
        printf("大端\n");
    return 0;
}

4. Storage analysis of floating point type in memory

The storage of floating-point type in memory is completely different from that of integer type.
For a 32-bit floating-point number, the first bit represents the sign bit (represented by S) , the next 8 bits represent the exponent bit (represented by E) , and the rest The bits of s represent significant bits (represented by M)
S: 0 for positive numbers, -1 for negative numbers .
E: Exponent plus 127 bits = its storage number, and these 8 bits represent an unsigned number
M: Its range is between 1 and 2. It is not necessary to store 1 when storing, and store the number after the decimal point, for
insert image description here
example :

	float i=6.5;
	二进制为110.1   1.101 *2^2
	s为0
	E为2+127=129
	M为101   后面位用0进行填充
	0 10000001 10100000000000000000000
	16进制进行表示
	0x40d00000

Let's take a look at the results of the computer operation, I
insert image description here
believe everyone should understand it!
There are several points to note here:
1. When E is all 0, the exponent bit is 1-127 (1-1023 (this is 64-bit)), and the significant bit M does not need to add 1, this one A number very close to ±0.
2. When E is all 1s, this is a very large number, ±infinity.
In addition to these two points of attention, how to store it, how to take it out.
For a 64-bit floating-point type, the 11 bits after the sign bit represent the exponent bit, plus1023, the rest is similar to the description of 32-bit floating point type.

Welfare: Bishop you study

Penguin skirt:720429262

Guess you like

Origin blog.csdn.net/m0_60598323/article/details/123146769