Comparing unsigned and signed numbers

1. Understand the conversion relationship between unsigned and signed numbers:

1.1 Extend understanding from a topic

#include <stdio.h>
int i;//i是全局变量,不初始化,默认是0
int main()
{
    i--;
    if (i > sizeof(i))
    {
        printf(">\n");
    }
    else
    {
        printf("<\n");
    }
    return 0;
}

 

Here we judge what should be output?

At that time, my idea was to start defining the plastic i variable and initialize it to 0. After the next statement is executed, i is -1, and sizeof(i) calculates the size of the plastic variable. The i variable occupies 4 bytes, and the answer is obvious. i == -1, sizeof(i) == 4 so should output '<'.

But the answer is '>'! ! Now let's get serious


1.2 What we need to know first is the return type of sizeof;

In C language, sizeof() is an operator for judging the data type or the length of an expression. Its return value is the number of bytes occupied by this data in memory; and this return value (number of bytes) is an untyped data that is an unsigned integer .

So what we have to think about here is how to judge unsigned integers and signed integers?


2. How are signed and unsigned types converted?

Concept: To put it simply, let’s take a one-byte char type as an example. Among the 8 bits of a signed type, the first bit represents the sign (0 is a positive number, 1 is a negative number), and the others represent the size of the data. The 8 bits in the unsigned type are used to indicate the size of the data.

int main()
{
    int b = -1;
    unsigned int a = 1;
    if (b > a)
    {
        printf(">");
    }
    else
    {
        printf("<");
    }
    return 0;
}

 Here we analyze this code (the answer is '>')

2.1 Data storage form in memory

In the computer, it is stored in the form of complement code . Here we study the storage form of integer. We know that the plastic type occupies four four sections, and each byte occupies 8 bits. Here we take signed integer type -1 and unsigned integer type 1 as an example:

-1

10000000 00000000 00000000 00000001 //Original code

11111111 11111111 11111111 11111110 //Inverse code (sign bit unchanged, other bits inverted)

11111111 11111111 11111111 11111111 //complement code (complement code +1)

 Let me explain here that the original code, inverse code and complement code of positive numbers are the same;

So here the storage form of the unsigned integer a is itself

1

00000000 00000000 00000000 00000001


2.2 Discussion on conversion between signed and unsigned types

After we have studied the storage form of each type in storage above, let's study whether the unsigned type is converted to a signed type, or is it converted to an untyped type?


Below we assume that an unsigned type is converted to a signed type:

-1

11111111 11111111 11111111 11111111

1

00000000 00000000 00000000 00000001

If the unsigned type 1 is converted to a signed type, and the highest bit is 0, it is +1 for a positive number.

Signed type -1, the highest bit is -1, and the negative number is (I don’t count~~~)

The result is obvious, the positive number must be greater than the negative number, the code should result in b<a, but the result we just calculated in the compiler is b>a, so the assumption is wrong.


Below we assume that a signed type is converted to an unsigned type:

-1

11111111 11111111 111111111 11111111

1

00000000 00000000 00000000 00000000

 Signed type-1 is converted to an unsigned type, and the highest bit is used to calculate the size of the data, which is a very large data;

The unsigned type remains unchanged and is still 1.

When we compare, we find that the results are valid, and we can draw a conclusion through the above experimental analysis;

.When there are signed and unsigned types in the expression, all operations are automatically converted to unsigned types. It can be seen that the operation priority of unsigned numbers is higher than that of signed numbers.

 

In fact, during these experiments, there are still many things we need to explore, such as: why the computer storage process should be stored in the form of complement code, and when comparing different types, it is necessary to use the concept of plastic promotion to solve it . This article is only based on the comparison of signed integer types and unsigned integer types.

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_68079583/article/details/124228699