learn_C_deep_5 (review the past and learn the new, deep understanding of sigend char a = -128, writing specification of unsigned int type)

Table of contents

learning new from the past

Understanding "unsigned int a = -10;"

How to understand big and small endian

The concept of big and small

How endianness affects data storage

Deep understanding of sigend char a = -128

Why is 10000000 -128 instead of -0

code exercise

Specification of the unsigned int type


learning new from the past

Understanding "unsigned int a = -10;"

After the last chapter, let's first review the knowledge of "unsigned int a = -10;".

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
	unsigned int a = -10;//编译无错误
	//-10转为二进制
	//原码:1000 0000 0000 0000 0000 0000 0000 1010
	//反码:1111 1111 1111 1111 1111 1111 1111 0101
	//补码:1111 1111 1111 1111 1111 1111 1111 0110
	//将二进制序列1111 1111 1111 1111 1111 1111 1111 0110 - 存到空间
	//再看类型为无符号整形,不看符号位,直接补码就是原码
	//1111 1111 1111 1111 1111 1111 1111 0110 - 4294967286
	printf("%u\n", a);//4294967286

	unsigned char b = -10;
	//将二进制序列1111 1111 1111 1111 1111 1111 1111 0110 - 存到空间
	//再看类型unsigned char
	//发生截断
	//1111 0110
	//再看类型为无符号char类型, 不看符号位, 直接补码就是原码
	//1111 0110 - 246
	printf("%u", b);
	return 0;
}

Let's review the knowledge of big and small

How to understand big and small endian

        Big and small ends can be understood by simulating the way binary numbers are stored in memory. Suppose we want to store a 16-bit binary number 0x1234 in memory, which is 4660 in decimal. In the big-endian byte order, the highest byte is stored in the lowest memory address, and the lowest byte is stored in the highest address. Therefore, in memory, the storage method of the binary number is as follows:

```

address content

0x00  0x12

0x01  0x34

```

It can be seen that 0x12 is stored in the lowest address 0x00, and 0x34 is stored in the highest address 0x01. In little-endian byte order, the lowest byte is stored in the lowest memory address, and the highest byte is stored in the highest address. Therefore, in memory, the storage method of the binary number is as follows:

```

address content

0x00  0x34

0x01  0x12

```

It can be seen that 0x34 is stored in the lowest address 0x00, and 0x12 is stored in the highest address 0x01.

Therefore, it can be understood that the big and small endian byte order means that when storing binary numbers in memory, the storage order of each byte is different . In big-endian byte order, the high-order byte comes first and the low-order byte follows; in little-endian byte order, the low-order byte comes first and the high-order byte comes last. These two byte orders will affect the storage and transmission of data, so you need to pay attention to the byte order on different hardware and software platforms.

The concept of big and small

Big Endian (Big Endian) is the high-order byte first , it stores the highest byte in the lowest memory address, and the lowest byte in the highest address, which is consistent with the order in which we read numbers , that is, the highest bit is on the far left, and the lowest bit is on the far right.

Little Endian (Little Endian) is the low-order byte first . It stores the lowest byte in the lowest memory address, and the highest byte in the highest address, which is consistent with the writing order we are used to. , that is, write numbers sequentially from left to right.

How endianness affects data storage

Deep understanding of sigend char a = -128

Why is 10000000 -128 instead of -0

        When using two's complement notation, 10000000 represents a negative number, not -0. This is because in the complement notation, the binary representation of a positive number is the same as the original code representation, and the binary representation of a negative number is the inverse of the original code of its absolute value plus 1. Therefore, the complement of 10000000 is 11111111 (inverted) + 1 = 10000000, which means -128, not -0. In two's complement notation, -0 has no meaning, and no corresponding binary representation exists. Because the original code, inverse code and complement code of 0 are all 00000000, and there is no difference between positive zero and negative zero in the complement code. Therefore, in two's complement notation, all binary numbers whose most significant bit (the sign bit) is 1 represent negative numbers, not -0.

code exercise

 Next, let's look at a few codes to see if we have mastered the above knowledge.

#include<stdio.h>
int main()
{
    char a[1000];
    int i = 0;
    for (i = 0; i < 1000; i++)
    {
        a[i] = -1 - i;
    }
    printf("%d", strlen(a));
    return 0;
}

This figure can make it easier for us to understand the value of the char data type. It is a circle (-1) -> (-128) -> 127 -> 0 -> (-1). 

 #include<stdio.h>
int main()
{
    int i = -20;
    unsigned int j = 10;
    printf("%d\n", i + j);
    printf("%u\n", i + j);
    return 0;
}

#include<stdio.h>
#include<windows.h>
int main()
{     unsigned int i = 0;     //infinite loop     for (i = 9; i >= 0; i--)     {         printf("%u \n", i);         Sleep(1000);//sleep for one second     }     return 0; }








Now try to explain this code by drawing a picture yourself!

#include<stdio.h>
#include<windows.h>
int main()
{     unsigned int i = 0;     //infinite loop     for (i = 0; i >= 0; i++)     {         printf("%u\n ", i);         Sleep(1000);//sleep for one second     }     return 0; }








The answer is in the back.

Specification of the unsigned int type

        When using the unsigne int type, it is recommended to add 'u' after the value when initializing

For example:

        unsigned int a = 10u;

        Because if unsigned int a = -10; the compiler will not report an error, but unsigned int a = -10u; will report an error: error C4146: Unary negative operator applied to unsigned type, the result is still unsigned type,

Guess you like

Origin blog.csdn.net/qq_64446981/article/details/130298093