简体   繁体   English

有符号/无符号int不匹配

[英]signed/unsigned int mismatch

The following program gives a signed/unsigned mismatch warning: 以下程序给出有符号/无符号不匹配警告:

#include <iostream>

int main()
{
unsigned int a = 2;
int b = -2;

if(a < b)
    std::cout << "a is less than b!";

return 0;
}

I'm trying to understand the problem when it comes to mixing signed and unsigned ints. 我试图了解混合有符号和无符号整数的问题。 From what I have been told, an int is typically stored in memory using two's complement. 据我所知,一个int通常使用二进制补码存储在内存中。

So, let's say I have the number 2. Based on what I understand it will be represented in memory like this: 因此,假设我的数字为2。根据我的理解,它将在内存中表示为:

00000000 00000000 00000000 00000010

And -2 will be represented as the one's compliment plus 1, or: 而-2将表示为一个人的恭维加1,或者:

11111111 11111111 11111111 11111110

With two's compliment there is no bit reserved for the sign like the "Sign-and-magnitude method". 有了二的恭维,就没有像“符号和幅度方法”那样为符号保留任何位。 If there is no sign bit, why are unsigned ints capable of storing larger positive numbers? 如果没有符号位,为什么没有符号的int能够存储更大的正数? What is an example of a problem which could occur when mixing signed/unsigned ints? 混合有符号/无符号整数时可能出现的问题的例子是什么?

I'm trying to understand the problem when it comes to mixing signed and unsigned ints. 我试图了解混合有符号和无符号整数的问题。

a < b

By the usual arithmetic conversions b is converted to an unsigned int , which is a huge number > a . 通过通常的算术转换, b会转换为unsigned int ,这是一个很大的数字> a

Here the expression a < b is the same as: 这里的表达式a < b与以下内容相同:

2U < (unsigned int) -2 which the same as: 2U < (unsigned int) -2 ,与:

2U < UINT_MAX - 1 (in most two's complement systems) which is 1 (true). 2U < UINT_MAX - 1 (在大多数的二进制补码系统中)为1 (真)。

With two's compliment there is no bit reserved for the sign like the "Sign-and-magnitude method". 有了二的恭维,就没有像“符号和幅度方法”那样为符号保留任何位。

In two's complement representation if the most significant bit of a signed quantity is 1 , the number is negative. 在二进制补码表示形式中,如果有符号数量的最高有效位为1 ,则该数字为负。

What would be the representation of 2 147 483 648 be? 2 147 483 648的表示形式是什么?

10000000 00000000 00000000 00000000

What would be the representation of -2 147 483 648 be? -2 147 483 648的表示形式是什么?

10000000 00000000 00000000 00000000

The same! 相同! Hence, you need a convention to know the difference. 因此,您需要约定以了解不同之处。 The convention is that the first bit is still used to decide the sign, just not using the naïve sign-magnitude method you would otherwise use. 习惯上,第一位仍用于确定符号,只是不使用否则会使用的朴素符号幅度方法。 This means every positive number starts with 0, leaving only 31 bits for the actual number. 这意味着每个正数都以0开头,而实际数只剩下31位。 This gives half the positive range of unsigned numbers. 这给出了无符号数字正数范围的一半。

This problem with your code is that the signed integer will be converted to unsigned. 您的代码的问题是有符号整数将被转换为无符号。 For example, -1 will become 4 294 967 295 (they have the same binary representation), and will be much larger than zero, instead of smaller. 例如,-1将变为4294967295(它们具有相同的二进制表示形式),并且将比零大很多,而不是小一点。 This is probably not what you expect. 这可能不是您所期望的。

An int can only store 2^32 different values (if it's 32bit), whether it is signed or unsigned . 一个int只能存储2 ^ 32个不同的值(如果是32bit),无论它是有signed还是unsigned So a signed int has 1/2 of that range below zero, and 1/2 of that range above zero. 因此,有signed int的范围是零以下的1/2,范围是零以上的1/2。 An unsigned int has that full range above zero. unsigned int完整范围大于零。

While they don't call the most significant bit of a signed int a 'sign bit', it can be treated that way. 尽管他们没有将有signed int的最高有效位称为“符号位”,但可以通过这种方式进行处理。

Isnt this as easy as (int)a < int(b)??? 这不像(int)a <int(b)一样容易吗??? Isnt C++ like you need to strongly do explicit type casting? 像您那样的C ++是否需要强烈进行显式类型转换?

Well, the -1 as signed int is -1 and as unsigned int is 65534, so the problem is with signed values where "-" is required. 好吧,-1的有符号int是-1,而无符号int是65534,所以问题出在有符号值的地方,其中需要“-”。 In case of returning -1 error code, with unsigned int that would be 65534 code. 如果返回-1错误代码,则unsigned int将为65534代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM