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

Directory

Learning 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


Learn the old and learn the new

understand“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;// compile without error
//-10 converted to binary
//Original code: 1000 0000 0000 0000 0000 0000 0000 1010
//Inverse code: 1111 1111 1111 1111 1111 1111 1111 0101
//Complement: 1111 1111 1111 1111 1111 1111 1111 0110
//Save the binary sequence 1111 1111 1111 1111 1111 1111 1111 0110 to space
// Look at the type again as unsigned integer, don't look at the sign bit, the direct complement is the original code
//1111 1111 1111 1111 1111 1111 1111 0110 - 4294967286
printf("%u\\
", a);//4294967286

unsigned char b = -10;
//Save the binary sequence 1111 1111 1111 1111 1111 1111 1111 0110 to space
//Look at the type unsigned char again
//Truncation occurs
//1111 0110
// Look at the unsigned char type again, don't look at the sign bit, the direct complement is the original code
//1111 0110-246
printf("%u", b);
return 0;
}

Let’s review the knowledge of big and small

How to understand big and small end

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 endian

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

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

How does endianness affect 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
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 picture 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
int main()
{
int i = -20;
unsigned int j = 10;
printf(“%d\\
“, i + j);
printf(“%u\\
“, i + j);
return 0;
}

#include
#include
int main()
{
unsigned int i = 0;
// infinite loop
for (i = 9; i >= 0; i–)
{
printf(“%u\\
“, i);
Sleep(1000);//Sleep for one second
}
return 0;
}

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

#include
#include
int main()
{
unsigned int i = 0;
// infinite loop
for (i = 0; i >= 0; i ++ )
{
printf(“%u\\
“, i);
Sleep(1000);//Sleep for one second
}
return 0;
}

The answer is in the back.

Writing specification of 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,

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge