无符号/有符号不匹配 [英] Unsigned/Signed Mismatch

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

问题描述

好的,这是关于标准的问题。在比较声明中对

未签名/签名的不匹配有什么看法:


char a = 3;

unsigned char b = 255;

if(a< b)


现在真正的答案是什么?如果a转换为无符号,则b> a。

但是,如果b转换为signed,则a> b。什么是正确的转换

(编译器应该做什么?)


-

MiniDisc_2k2

要回复,请将nospam.com替换为cox dot net。


---

[comp.std.c ++经过审核。要提交文章,请尝试发布]

[您的新闻阅读器。如果失败,请使用mailto:st ***** @ ncar.ucar.edu]

[---请在发布前查看常见问题解答。 ---]

[常见问题: http://www.jamesd.demon.co.uk/csc/faq.html ]

Okay, here''s a question about the standard. What does it say about
unsigned/signed mismatches in a comparison statement:

char a = 3;
unsigned char b = 255;
if (a<b)

Now what''s the real answer here? If a is converted to unsigned, then b>a.
But, if b is converted to signed,then a>b. What''s the correct coversion
(what is the compiler supposed to do?)

--
MiniDisc_2k2
To reply, replace nospam.com with cox dot net.

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:st*****@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

推荐答案

Victor Bazarov <五******** @ attAbi.com>在消息中写道

news:vg ************ @ corp.supernews.com ...
"Victor Bazarov" <v.********@attAbi.com> wrote in message
news:vg************@corp.supernews.com...

整体促销活动是适用于这两个操作数。如果''int''可以表示源类型的
值,或''unsigned',则'/''和''b''都会转换为''int'' ' 除此以外。在
通常情况下,sizeof(int)> 1,'a''和'b''都应成为''int'',然后进行比较。恕我直言,不需要错误匹配警告。如果sizeof(int)== 1,那么并非所有无符号的
char值都可以用''int''表示。在这种情况下,'a''
和'b''都将转换为''unsigned int''。在这种情况下,如果
''''为负数,则无法获得预期的比较
通过复制形成否定的'a''的无符号值
位模式(无论结果如何)。想象一下,如果
''''是-1,''b''是255,而sizeof(int)== 1,那么你就是
一个二的补码机器,无符号(a)将至少为65535.
你会得到(a< b)== false。在这种情况下,警告
是有保证的,恕我直言。

Integral promotions are applied to both operands here. Both
''a'' and ''b'' are converted to ''int'', if ''int'' can represent the
values of the source type, or to ''unsigned'' otherwise. In
a usual scenario where sizeof(int) > 1, both ''a'' and ''b'' shall
become ''int'' and then compared. There is no need for the mis-
match warning, IMHO. If sizeof(int) == 1, then not all unsigned
char values can be represented by ''int''. In such case both ''a''
and ''b'' will be converted to ''unsigned int''. In that case, if
''a'' is negative, you will not get the intended comparison for
the unsigned value for a negative ''a'' will be formed by copying
the bit pattern (whatever that results into). Imagine that if
''a'' is -1, and ''b'' is 255, and sizeof(int)==1, and you''re on
a two''s complement machine, unsigned(a) will be at least 65535.
And you will get (a < b) == false. In this case the warning
is warranted, IMHO.







在您的假设情景中(sizeof( char)== sizeof(int))如果你有一个

签名的字符,它将被转换为long,而不是unsigned int。



-

Ioannis


*编程页面: http://www.noicys.freeurl.com

*替代网址1: http://run.to/noicys

*替代网址2: http://www.noicys.cjb.net



?

In your hypothetical scenario (sizeof(char)==sizeof(int)) if you have a
signed char it will be converted to long, not to unsigned int.


--
Ioannis

* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net


" " MiniDisc_2k2"" <马****** @ nospam.com>在消息中写道

news:%jjPa.31
""MiniDisc_2k2"" <Ma******@nospam.com> wrote in message
news:%jjPa.31


zd4.2@lakeread02 ...
zd4.2@lakeread02...
好的,这里是's关于标准的问题。在比较声明中对
未签名/签名不匹配的说法是什么:

char a = 3;
unsigned char b = 255;
if(a< b )

现在真正的答案是什么?如果a转换为无符号,则b> a。
但是,如果b转换为signed,则a> b。什么是正确的转换
(编译器应该做什么?)
Okay, here''s a question about the standard. What does it say about
unsigned/signed mismatches in a comparison statement:

char a = 3;
unsigned char b = 255;
if (a<b)

Now what''s the real answer here? If a is converted to unsigned, then b>a.
But, if b is converted to signed,then a>b. What''s the correct coversion
(what is the compiler supposed to do?)



首先要记住char(与其他plain整数<不同) br />
types)可以是有符号或无符号的。如果你想要

中的两个中的一个,你必须明确声明它(例如signed char a = 3)。

假设你希望你的上述代码是:

签名char a = 3;

unsigned char b = 255;

if(a< b)



表达式求值期间,unsigned char和signed char都转换为int。

总而言之,无符号的signed char转换为int。


-

Ioannis


*编程页面: http://www.noicys.freeurl.com

*替代网址1: http://run.to/noicys

*替代网址2: http://www.noicys.cjb.net


At first keep in mind that char (differently from other "plain" integer
types) can be either signed or unsigned. If you want one of the two in
specific, you have to declare it explicitly (e.g. signed char a=3).
Assuming you wish your above code to be:
signed char a = 3;
unsigned char b = 255;
if (a<b)
Both unsigned char and signed char get converted to int during the
expression evaluation.
So in summary, a either signed char either unsigned gets converted to int.

--
Ioannis

* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net


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

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