有符号整数和无符号整数的区别是什么

时间:2022-06-24 11:37:05

What is the difference between signed and unsigned int?

有符号整数和无符号整数的区别是什么?

4 个解决方案

#1


75  

As you are probably aware, ints are stored internally in binary. Typically an int contains 32 bits, but in some environments might contain 16 or 64 bits (or even a different number, usually but not necessarily a power of two).

您可能已经知道,int类型是在二进制文件中存储的。一般来说,int包含32位,但在某些环境中可能包含16或64位(甚至是不同的数字,通常但不一定是2的幂)。

But for this example, let's look at 4-bit integers. Tiny, but useful for illustration purposes.

但是在这个例子中,让我们看看4位整数。很小,但对演示很有用。

Since there are four bits in such an integer, it can assume one of 16 values; 16 is two to the fourth power, or 2 times 2 times 2 times 2. What are those values? The answer depends on whether this integer is a signed int or an unsigned int. With an unsigned int, the value is never negative; there is no sign associated with the value. Here are the 16 possible values of a four-bit unsigned int:

由于这样一个整数中有4位,它可以假设16个值中的一个;16是2的四次方,或者2乘以2乘以2乘以2乘以2。这些值是什么?答案取决于这个整数是带符号整型还是无符号整型。没有与该值相关联的符号。这里是一个4位无符号整数的16个可能值:

bits  value
0000    0
0001    1
0010    2
0011    3
0100    4
0101    5
0110    6
0111    7
1000    8
1001    9
1010   10
1011   11
1100   12
1101   13
1110   14
1111   15

... and Here are the 16 possible values of a four-bit signed int:

…这里是16个可能值的4位有符号整数:

bits  value
0000    0
0001    1
0010    2
0011    3
0100    4
0101    5
0110    6
0111    7
1000   -8
1001   -7
1010   -6
1011   -5
1100   -4
1101   -3
1110   -2
1111   -1

As you can see, for signed ints the most significant bit is 1 if and only if the number is negative. That is why, for signed ints, this bit is known as the "sign bit".

如您所见,对于有符号的ints,当且仅当数字为负数时,最重要的位是1。这就是为什么,对于已签名的ints,这个位被称为“符号位”。

#2


10  

Sometimes we know in advance that the value stored in a given integer variable will always be positive-when it is being used to only count things, for example. In such a case we can declare the variable to be unsigned, as in, unsigned int num student;. With such a declaration, the range of permissible integer values (for a 32-bit compiler) will shift from the range -2147483648 to +2147483647 to range 0 to 4294967295. Thus, declaring an integer as unsigned almost doubles the size of the largest possible value that it can otherwise hold.

有时我们提前知道,在给定的整数变量中存储的值总是正的——例如,当它被用于只计算事物时。在这种情况下,我们可以声明变量是无符号的,比如,无符号int num student;通过这样的声明,允许的整数值范围(32位编译器)将从范围-2147483648转到+2147483647到0到4294967295。因此,声明一个无符号整数几乎是它所能容纳的最大可能值的两倍。

#3


9  

int and unsigned int are two distinct integer types. (int can also be referred to as signed int, or just signed; unsigned int can also be referred to as unsigned.)

int和unsigned int是两种不同的整数类型。(int也可以称为有符号int,或者只是有符号的int;无符号int也可以称为无符号int。

As the names imply, int is a signed integer type, and unsigned int is an unsigned integer type. That means that int is able to represent negative values, and unsigned int can represent only non-negative values.

顾名思义,int是带符号整型,无符号整型是无符号整型。这意味着int可以表示负值,而无符号int只能表示非负值。

The C language imposes some requirements on the ranges of these types. The range of int must be at least -32767 .. +32767, and the range of unsigned int must be at least 0 .. 65535. This implies that both types must be at least 16 bits. They're 32 bits on many systems, or even 64 bits on some. int typically has an extra negative value due to the two's-complement representation used by most modern systems.

C语言对这些类型的范围施加了一些要求。int的范围必须至少是-32767。+32767,且无符号整数的范围必须至少为0。65535年。这意味着这两种类型必须至少是16位。它们在许多系统上是32位,甚至在某些系统上是64位。int通常有一个额外的负值,这是由于大多数现代系统使用的二补表示。

Perhaps the most important difference is the behavior of signed vs. unsigned arithmetic. For signed int, overflow has undefined behavior. For unsigned int, there is no overflow; any operation that yields a value outside the range of the type wraps around, so for example UINT_MAX + 1 == 0.

也许最重要的区别是有符号算术和无符号算术的行为。对于有符号int,溢出具有未定义的行为。对于无符号int,不存在溢出;任何在类型范围之外产生值的操作,例如UINT_MAX + 1 = 0。

Any integer type, either signed or unsigned, models a subrange of the infinite set of mathematical integers. As long as you're working with values within the range of a type, everything works. When you approach the lower or upper bound of a type, you encounter a discontinuity, and you can get unexpected results. For signed integer types, the problems occur only for very large negative and positive values, exceeding INT_MIN and INT_MAX. For unsigned integer types, problems occur for very large positive values and at zero. This can be a source of bugs. For example, this is an infinite loop:

任何有符号或无符号的整数类型,都为无限的数学整数集建模。只要您使用类型范围内的值,一切都可以工作。当你接近一个类型的下界或上界时,你会遇到一个不连续,你会得到意想不到的结果。对于有符号整数类型,问题只出现在非常大的负值和正值,超过INT_MIN和INT_MAX。对于无符号整数类型,问题发生在非常大的正值和零值。这可能是bug的来源。例如,这是一个无限循环:

for (unsigned int i = 10; i >= 0; i --) [
    printf("%u\n", i);
}

because i is always greater than or equal to zero; that's the nature of unsigned types. (Inside the loop, when i is zero, i-- sets its value to UINT_MAX.)

因为i总是大于或等于0;这就是无符号类型的本质。(在循环内部,当i为0时,i——将其值设置为UINT_MAX。)

#4


8  

In laymen's terms an unsigned int is an integer that can not be negative and thus has a higher range of positive values that it can assume. A signed int is an integer that can be negative but has a lower positive range in exchange for more negative values it can assume.

在外行人的术语中,无符号整型是一个不能为负的整数,因此它可以假定具有更高的正值范围。有符号整数是一个整数,它可以是负数,但是它的正数范围更小,以换取它可以假定的更多的负值。

#1


75  

As you are probably aware, ints are stored internally in binary. Typically an int contains 32 bits, but in some environments might contain 16 or 64 bits (or even a different number, usually but not necessarily a power of two).

您可能已经知道,int类型是在二进制文件中存储的。一般来说,int包含32位,但在某些环境中可能包含16或64位(甚至是不同的数字,通常但不一定是2的幂)。

But for this example, let's look at 4-bit integers. Tiny, but useful for illustration purposes.

但是在这个例子中,让我们看看4位整数。很小,但对演示很有用。

Since there are four bits in such an integer, it can assume one of 16 values; 16 is two to the fourth power, or 2 times 2 times 2 times 2. What are those values? The answer depends on whether this integer is a signed int or an unsigned int. With an unsigned int, the value is never negative; there is no sign associated with the value. Here are the 16 possible values of a four-bit unsigned int:

由于这样一个整数中有4位,它可以假设16个值中的一个;16是2的四次方,或者2乘以2乘以2乘以2乘以2。这些值是什么?答案取决于这个整数是带符号整型还是无符号整型。没有与该值相关联的符号。这里是一个4位无符号整数的16个可能值:

bits  value
0000    0
0001    1
0010    2
0011    3
0100    4
0101    5
0110    6
0111    7
1000    8
1001    9
1010   10
1011   11
1100   12
1101   13
1110   14
1111   15

... and Here are the 16 possible values of a four-bit signed int:

…这里是16个可能值的4位有符号整数:

bits  value
0000    0
0001    1
0010    2
0011    3
0100    4
0101    5
0110    6
0111    7
1000   -8
1001   -7
1010   -6
1011   -5
1100   -4
1101   -3
1110   -2
1111   -1

As you can see, for signed ints the most significant bit is 1 if and only if the number is negative. That is why, for signed ints, this bit is known as the "sign bit".

如您所见,对于有符号的ints,当且仅当数字为负数时,最重要的位是1。这就是为什么,对于已签名的ints,这个位被称为“符号位”。

#2


10  

Sometimes we know in advance that the value stored in a given integer variable will always be positive-when it is being used to only count things, for example. In such a case we can declare the variable to be unsigned, as in, unsigned int num student;. With such a declaration, the range of permissible integer values (for a 32-bit compiler) will shift from the range -2147483648 to +2147483647 to range 0 to 4294967295. Thus, declaring an integer as unsigned almost doubles the size of the largest possible value that it can otherwise hold.

有时我们提前知道,在给定的整数变量中存储的值总是正的——例如,当它被用于只计算事物时。在这种情况下,我们可以声明变量是无符号的,比如,无符号int num student;通过这样的声明,允许的整数值范围(32位编译器)将从范围-2147483648转到+2147483647到0到4294967295。因此,声明一个无符号整数几乎是它所能容纳的最大可能值的两倍。

#3


9  

int and unsigned int are two distinct integer types. (int can also be referred to as signed int, or just signed; unsigned int can also be referred to as unsigned.)

int和unsigned int是两种不同的整数类型。(int也可以称为有符号int,或者只是有符号的int;无符号int也可以称为无符号int。

As the names imply, int is a signed integer type, and unsigned int is an unsigned integer type. That means that int is able to represent negative values, and unsigned int can represent only non-negative values.

顾名思义,int是带符号整型,无符号整型是无符号整型。这意味着int可以表示负值,而无符号int只能表示非负值。

The C language imposes some requirements on the ranges of these types. The range of int must be at least -32767 .. +32767, and the range of unsigned int must be at least 0 .. 65535. This implies that both types must be at least 16 bits. They're 32 bits on many systems, or even 64 bits on some. int typically has an extra negative value due to the two's-complement representation used by most modern systems.

C语言对这些类型的范围施加了一些要求。int的范围必须至少是-32767。+32767,且无符号整数的范围必须至少为0。65535年。这意味着这两种类型必须至少是16位。它们在许多系统上是32位,甚至在某些系统上是64位。int通常有一个额外的负值,这是由于大多数现代系统使用的二补表示。

Perhaps the most important difference is the behavior of signed vs. unsigned arithmetic. For signed int, overflow has undefined behavior. For unsigned int, there is no overflow; any operation that yields a value outside the range of the type wraps around, so for example UINT_MAX + 1 == 0.

也许最重要的区别是有符号算术和无符号算术的行为。对于有符号int,溢出具有未定义的行为。对于无符号int,不存在溢出;任何在类型范围之外产生值的操作,例如UINT_MAX + 1 = 0。

Any integer type, either signed or unsigned, models a subrange of the infinite set of mathematical integers. As long as you're working with values within the range of a type, everything works. When you approach the lower or upper bound of a type, you encounter a discontinuity, and you can get unexpected results. For signed integer types, the problems occur only for very large negative and positive values, exceeding INT_MIN and INT_MAX. For unsigned integer types, problems occur for very large positive values and at zero. This can be a source of bugs. For example, this is an infinite loop:

任何有符号或无符号的整数类型,都为无限的数学整数集建模。只要您使用类型范围内的值,一切都可以工作。当你接近一个类型的下界或上界时,你会遇到一个不连续,你会得到意想不到的结果。对于有符号整数类型,问题只出现在非常大的负值和正值,超过INT_MIN和INT_MAX。对于无符号整数类型,问题发生在非常大的正值和零值。这可能是bug的来源。例如,这是一个无限循环:

for (unsigned int i = 10; i >= 0; i --) [
    printf("%u\n", i);
}

because i is always greater than or equal to zero; that's the nature of unsigned types. (Inside the loop, when i is zero, i-- sets its value to UINT_MAX.)

因为i总是大于或等于0;这就是无符号类型的本质。(在循环内部,当i为0时,i——将其值设置为UINT_MAX。)

#4


8  

In laymen's terms an unsigned int is an integer that can not be negative and thus has a higher range of positive values that it can assume. A signed int is an integer that can be negative but has a lower positive range in exchange for more negative values it can assume.

在外行人的术语中,无符号整型是一个不能为负的整数,因此它可以假定具有更高的正值范围。有符号整数是一个整数,它可以是负数,但是它的正数范围更小,以换取它可以假定的更多的负值。