减去无符号整数 [英] Subtracting unsigned ints

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

问题描述




我有一个比较两个无符号整数的函数,并返回它们的

差异,以确定两者中哪一个更大。


int比较(unsigned int time_left,unsigned int time_right)

{

return(time_left - time_right);

}


用法 -


if(比较(time_left,time_right)> 0)

//时机已到。


问题是,当time_left为5且time_right为10时,我得-5,

但是如果time_left是5,time_right是0xc0000005(数字大于

比5)当我希望得到一个负数时,我得到一个正值。


我理解为什么我得到了这个正值 - 因为在time_right中设置了否定位

,但是如何更改我的代码以便获得所需的结果




感谢您的帮助,


Barry。

解决方案

bg***@yahoo.com 写道:



int Compare(unsigned int time_left,unsigned int time_right)
{
返回(time_left - time_right);
}

用法 -

if(比较(time_left,time_right)> 0)
//时机已到。

问题是,当time_left为5且time_right为10时,我得-5,
但是如果time_left是5并且time_right是0xc0000005(比5更大的数字)当我希望得到一个否定的数字时,我得到一个正值。

我理解为什么我得到这个正值 - 因为否定位
在time_right中设置,但是如何更改我的代码以便获得所需的结果。




如果确切的差异不对问题;


返回(time_left> time_right);





返回(( time_left> time_right)?1:0);


-

Ian Collins。


b ... @ yahoo.com写道:



我有一个函数比较两个无符号整数并返回它们的差异以便建立这是两者中较大的一个。


你不能减去两个无符号整数并产生负数。

int比较(unsigned int time_left,unsigned int time_right)
{
返回(time_left - time_right);


用法 -

if(比较(time_left,time_right)> 0)
//时机已到。

问题是,当time_left为5且time_right为10时,我得-5,
但是如果time_left是5而time_right是0xc0000005(一个数字)当我希望得到一个否定的时候,我得到一个正面的价值。


尝试...


if(time_left< time_right)返回-1;

if(time_left > time_right)返回+1;

返回0;


或者......


返回(time_left < time_right)? -1:(time_left> time_right);


或者......


return(time_left> time_right) - (time_left< time_right);

我理解为什么我得到这个正值 - 因为在时间上设置了否定的位
,...




否,time_right是unsigned int。有_no_负(符号)位。


问题是对无符号整数执行算术

模数比UINT_MAX多一个。
< br $> b $ b -

彼得


bg *** @ yahoo.com 写道:



我有一个比较两个无符号整数的函数返回它们之间的差异,以确定两者中哪一个更大。

{
return(time_left - time_right);

用法 -

if(比较(time_left,time_right)> 0)
//时机已到。

问题是当time_left为5且time_right为10时,我得-5,
但是如果time_left是5而time_right是0xc0000005(数字大于5)当我希望得到一个否定的时候,我得到一个积极的价值。

我理解为什么我得到这个正值 - 因为否定位
是在time_right中设置的,但是如何更改我的代码以便获得所需的结果。




你设置的并不重要。它应该是一个unsigned int,所以

它被提升到高于int的东西(假设int有4

字节)才能存储正数值。他们可能会将

提升为多,所以你在两个长期的
参数之间存在差异。但是,即使无符号值

不适合int,你也会返回int。


-

Ioan - Ciprian Tandau

tandau _at_ freeshell _dot_ org(希望现在还不算太晚)

(......而且它仍然可以......) />


Hi,

I have a function which compares two unsigned ints and returns their
difference in order to establish which is the greater of the two.

int Compare(unsigned int time_left, unsigned int time_right)
{
return (time_left - time_right);
}

usage -

if( Compare(time_left, time_right) > 0)
// the time has come.

The problem is that when time_left is 5 and time_right is 10, I get -5,
but if time_left is 5 and time_right is 0xc0000005 (a number greater
than 5) I get a positive value when I wish to get a negitive one.

I understand why I get this positive value - because the negaitve bit
is set in time_right, but how can I change my code so that I get the
desired results.

Thanks for your help,

Barry.

解决方案

bg***@yahoo.com wrote:

Hi,

I have a function which compares two unsigned ints and returns their
difference in order to establish which is the greater of the two.

int Compare(unsigned int time_left, unsigned int time_right)
{
return (time_left - time_right);
}

usage -

if( Compare(time_left, time_right) > 0)
// the time has come.

The problem is that when time_left is 5 and time_right is 10, I get -5,
but if time_left is 5 and time_right is 0xc0000005 (a number greater
than 5) I get a positive value when I wish to get a negitive one.

I understand why I get this positive value - because the negaitve bit
is set in time_right, but how can I change my code so that I get the
desired results.



If the exact difference doesn''t matter;

return (time_left > time_right);

or

return ((time_left > time_right) ? 1 : 0);

--
Ian Collins.


b...@yahoo.com wrote:

Hi,

I have a function which compares two unsigned ints and returns their
difference in order to establish which is the greater of the two.
You can''t subtract two unsigned ints and yield a negative number.
int Compare(unsigned int time_left, unsigned int time_right)
{
return (time_left - time_right);
}

usage -

if( Compare(time_left, time_right) > 0)
// the time has come.

The problem is that when time_left is 5 and time_right is 10, I get -5,
but if time_left is 5 and time_right is 0xc0000005 (a number greater
than 5) I get a positive value when I wish to get a negitive one.
Try...

if (time_left < time_right) return -1;
if (time_left > time_right) return +1;
return 0;

Or...

return (time_left < time_right) ? -1 : (time_left > time_right);

Or...

return (time_left > time_right) - (time_left < time_right);

I understand why I get this positive value - because the negaitve bit
is set in time_right, ...



No, time_right is an unsigned int. There is _no_ negative (sign) bit.

The problem is that arithmetic on unsigned ints is performed
modulo one more than UINT_MAX.

--
Peter


bg***@yahoo.com writes:

Hi,

I have a function which compares two unsigned ints and returns their
difference in order to establish which is the greater of the two.

int Compare(unsigned int time_left, unsigned int time_right)
{
return (time_left - time_right);
}

usage -

if( Compare(time_left, time_right) > 0)
// the time has come.

The problem is that when time_left is 5 and time_right is 10, I get -5,
but if time_left is 5 and time_right is 0xc0000005 (a number greater
than 5) I get a positive value when I wish to get a negitive one.

I understand why I get this positive value - because the negaitve bit
is set in time_right, but how can I change my code so that I get the
desired results.



It doesn''t matter what you set. It''s supposed to be an unsigned int so
it gets promoted to something higher than int (assuming that int has 4
bytes) to be able to store the positive value. They probably get
promoted to long so you have a difference between two long
arguments. But, you are returning int even if the unsigned value
doesn''t fit in an int.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it''s not too late)
(... and that it still works...)


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

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