无符号到有符号的转换 [英] unsigned to signed conversion

查看:928
本文介绍了无符号到有符号的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下内容:

  #include< iostream> 

int main(){

unsigned int x = 3;
unsigned int y = 5;

std :: cout<< a:<< x - y< std :: endl;

std :: cout<< b:<< ((int)x)-y < std :: endl;

std :: cout<< c:< x - ((int)y) std :: endl;

std :: cout<< d:<< ((int)x) - ((int)y) std :: endl;

}

$ g ++ -Wconversion -Wall uint_stackoverflow.cc -o uint_stackoverflow&& ./uint_stackoverflow
a:4294967294
b:4294967294
c:4294967294
d:-2

我理解为什么a不能给出预期的结果。但是为什么b和c失败困惑我。对于b,我认为在将x转换为int后,结果将再次是int。



你能请我吗? b
$ b

编辑:编译器不应该警告吗? g ++(Ubuntu / Linaro 4.4.4-14ubuntu5)4.4.5



感谢,



>

解决方案

在算术运算中,如果任何操作数 unsigned >其他操作数转换为 unsigned (如果签名), unsigned 也。



code> signed 然后做操作不改变操作数的位表示。在二进制补码结构(即每个现代架构)上,(int)x 具有与 x 只有在十进制系统中计算其时,其解释才会更改。但重要的是,对位表示(对其在十进制系统中的)执行算术运算。由于转换不改变位表示,结果的位表示也不会改变。



C ++ 03标准在§5/ 9中说:


许多二进制运算符需要
算术或枚举操作数
类型导致转换和产生
result类型以类似的方式。
的目的是产生一个公共类型,
也是结果的类型。
这种模式称为通常的
算术转换,其中
定义如下:



[...] >

否则,如果任一操作数为
无符号,则另一个将
转换为无符号



Consider the following:

#include <iostream>

int main() {

    unsigned int x = 3;
    unsigned int y = 5;

    std::cout << "a: " << x - y        << std::endl;

    std::cout << "b: " << ((int)x) - y << std::endl;

    std::cout << "c: " << x - ((int)y) << std::endl;

    std::cout << "d: " << ((int)x) - ((int)y) << std::endl;

}

$ g++ -Wconversion -Wall uint_stackoverflow.cc -o uint_stackoverflow && ./uint_stackoverflow
a: 4294967294
b: 4294967294
c: 4294967294
d: -2

I understand why "a" doesn't give the expected result. But why "b" and "c" fail puzzles me. For "b" I thought after casting "x" to "int" the result will be "int" again.

Could you please enlighten me?

edit: Shouldn't the compiler warn? g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5

Thanks,

Somebody

解决方案

In arithmetic operations, if any of the operand is unsigned, the other operand converts to unsigned (if its signed), and the result of the operations will be unsigned also.

Also, casting unsigned to signed and then doing the operation doesn't change the bit representation of the operand. On a two's complement architecture (i.e almost every modern architecture), (int)x has same bit representation as x has, only their interpretation changes when calculating their value in decimal system. But the important point is that the arithmetic operation is performed on the bit representations (not on their values in decimal system). And since the casting doesn't change the bit representation, the bit representation of the result will also NOT change.

C++03 Standard says in §5/9:

Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:

[...]

Otherwise, if either operand is unsigned, the other shall be converted to unsigned.

这篇关于无符号到有符号的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆