为什么我必须处理有符号/无符号比较? [英] Why do I have to handle signed/unsigned comparisons?

查看:102
本文介绍了为什么我必须处理有符号/无符号比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另请参见 http://social.msdn.microsoft.com/Forums/zh-CN/vclanguage/thread/9c0b240a-4122-4d33-a7b3-9a7549adf0e7/ 上面有错误标记的解决方案.

See Also http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/9c0b240a-4122-4d33-a7b3-9a7549adf0e7/  which has a solution falsely marked on it.

有人可以告诉我为什么十年以后,编译器仍然不知道如何正确处理带符号/无符号的比较,必须让开发人员为他们处理吗?这是为解决问题而提出的一组宏,它们变成了 使用起来很丑,因为它们使您重新指定类型(编译器已经可以知道的东西)

Can someone tell me why after a decade plus, compilers still can't figure out how to correctly handle signed/usigned comparisons, and must make the developer handle it for them?   Here are set of proposed macros to solve the problem, they become ugly in use, becuase they make you re-specify the type (something the compiler would already be able to know)

是要比较的值,at和bt是类型,而b类型是要比较的值的类型.是的,在某些情况下,不使用一种或另一种,但是为了一致性,我同时提供了两者.

in the macros below a and b are the values to compare, at and bt are a type and b type that is the types of the values to compare.  Yes in certain cases one or the other type is not used, but for consistancy I supply both...

------------------

---------------


#ifdef _MSC_VER
#define COMPILER_THROWS_SIGNED_UNSIGNED_MISMATCH
#endif


#ifdef _MSC_VER
#define COMPILER_THROWS_SIGNED_UNSIGNED_MISMATCH
#endif

#ifdef COMPILER_THROWS_SIGNED_UNSIGNED_MISMATCH

#ifdef COMPILER_THROWS_SIGNED_UNSIGNED_MISMATCH

#define SUS_GT(a,at,b,bt) ((((a)< 0)?0:((((bt)a)>(b))))
#define USS_GT(a,at,b,bt)  ((((b)< 0)?1:(((a)>((at)b)))

#define SUS_GT(a,at,b,bt)   (((a)<0)?0:(((bt)a)>(b)))
#define USS_GT(a,at,b,bt)   (((b)<0)?1:((a)>((at)b)))

#define SUS_LT(a,at,b,bt) ((((a)< 0)?1:((((bt)a)<(b)))
#define USS_LT(a,at,b,bt)  ((((b)< 0)?0:((a)<((at)b)))

#define SUS_LT(a,at,b,bt)   (((a)<0)?1:(((bt)a)<(b)))
#define USS_LT(a,at,b,bt)   (((b)<0)?0:((a)<((at)b)))

#define SUS_GTE(a,at,b,bt)  ((((a)< 0)?0:((((bt)a)&== b)))
#define USS_GTE(a,at,b,bt)  ((((b)< 0)?1:(((a)> =((at)b))))

#define SUS_GTE(a,at,b,bt)  (((a)<0)?0:(((bt)a)>=(b)))
#define USS_GTE(a,at,b,bt)  (((b)<0)?1:((a)>=((at)b)))

#define SUS_LTE(a,at,b,bt) ((((a)< 0)?1:((((bt)a)< =(b)))
#define USS_LTE(a,at,b,bt)  ((((b)< 0)?0:(((a)< =((at)b))))

#define SUS_LTE(a,at,b,bt)  (((a)<0)?1:(((bt)a)<=(b)))
#define USS_LTE(a,at,b,bt)  (((b)<0)?0:((a)<=((at)b)))


#else
#定义SUS_GT(a,at,b,bt) ((a)>(b))
#define USS_GT(a,at,b,bt)  ((a)>(b))


#else
#define SUS_GT(a,at,b,bt)   ((a)>(b))
#define USS_GT(a,at,b,bt)   ((a)>(b))

#define SUS_LT(a,at,b,bt) ((a)<(b))
#define USS_LT(a,at,b,bt)  ((a)<(b))

#define SUS_LT(a,at,b,bt)   ((a)<(b))
#define USS_LT(a,at,b,bt)   ((a)<(b))

#define SUS_GTE(a,at,b,bt)  ((a)> =(b))
#define USS_GTE(a,at,b,bt)  ((a)> =(b))

#define SUS_GTE(a,at,b,bt)  ((a)>=(b))
#define USS_GTE(a,at,b,bt)  ((a)>=(b))

#define SUS_LTE(a,at,b,bt) ((a)< =(b))
#define USS_LTE(a,at,b,bt)  ((a)< =(b))
#endif

#define SUS_LTE(a,at,b,bt)  ((a)<=(b))
#define USS_LTE(a,at,b,bt)  ((a)<=(b))
#endif

-------------------

-------------------

 

SUS_是未签名的

USS_是未签名的

_GT大于

_LT小于

_GTE大于或等于

_LTE小于或等于

 

为什么它不给我有关有符号/无符号相等的警告(嗯,也许我不再有那些符号了)?我的意思是

and why doesn't it give me warnings about signed/unsigned equality (hmm maybe I don't have any of those anymore)?  I mean

-3L == 0xFFFFFFFDUL 也不对.

-3L == 0xFFFFFFFDUL  isn't right either.

 

------------------

---------------

但实际上-为什么我必须手动执行此操作?

But really - why should I have to do this manually? 

 

有时-与您一起使用的API给您提供了必须解决的冲突类型...强制转换是可怕的,因为当类型更改并且事物迁移时,很多年都看不到问题了.甚至我的宏都不完整,因为从技术上讲,我应该 测试传递给"at"和"bt"的类型(a类型和b类型)实际上分别是a和b的类型.

And sometimes - the API you work with gives you conflicting types that you must resolve... casts are horrible, because when types change and things migrate, problems go unseen for many year.  Even my macros are incomplete, because technically I should test that the types passed for 'at' and 'bt' (a-type and b-type) actually are the types of a and b respectively.

推荐答案

这真的不是编译器无法解决的问题理解这些概念不兼容.负数的概念以最高有效位(MSB)为中心.当MSB设置为有符号整数时,表示该数字为负数.在 如果是无符号数字,则设置的所有MSB均表示该数字很大. (例如,对于一个字节....这意味着该数字大于128.)其他位在负数上的行为也有所不同,例如,当所有位都在 设置一个有符号整数(ON),在所有情况下该数字均为-1.如果这些数字没有符号,则该值为最大正值,而不是最小负值.

It really isn't an issue of compilers being unable to understand; the concepts are not compatible. The notion of a negative number centers on the Most Significant Bit (MSB). When the MSB is set on a signed integer, it indicates the number is negative. In the case of an unsigned number, all the MSB being set means is that the number is large. (In the case of a byte....it means the number is greater than 128, for example.) The other bits behave differently on negative numbers, such that when all the bits in a signed integer are set (ON), the number is -1 in all cases. If those numbers were unsigned, the value would be the maximum positive value, and not the minimum negative value.

每次您进行比较时,都将要求编译器对数字进行假设.代替程序员明确说明,编译器只能猜测您的意图,并且您可能不会在意结果. (至少不是全部 时间.)

It would require the compiler to make an assumption about the numbers every time you made a comparison. In lieu of the programmer making it clear, the compiler can only guess your intent, and you would likely not care for the result. (At least not all the time.)


这篇关于为什么我必须处理有符号/无符号比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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