一元否定运算符问题 [英] unary negation operator question

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

问题描述

我在新闻组中搜索了这个并找到了答案但是

想要确定是因为出现了什么。


我想要绝对的一个''short int''的价值所以为了避免

溢出的危险我正在做以下几点:


short int x = -4; / *想要abs(x)* /

unsigned short int abs_val;


....


/ *已执行检查以确保x为负* /

abs_val = - (unsigned short int)x;


我认为以上是我应该如何安全地获得绝对值

的负数。我想证实这一点,因为我的Lint包

表示有一个:

符号丢失(赋值)(无符号短整数)。 />

谢谢!

I searched through the newsgroup for this and found the answer but
wanted to make sure because of something that came up.

I want the absolute value of a ''short int'' so to avoid the dangers of
overflow I am doing the follow:

short int x = -4; /* want abs(x) */
unsigned short int abs_val;

....

/* already performed check to make sure x is negative */
abs_val = -(unsigned short int)x;

I take it that the above is how I should safely get the absolute value
of a negative number. I want to confirm this because my Lint package
says that there is a:
"Loss of sign(assignment)(int to unsigned short)."

Thanks!

推荐答案

joshc写道:
joshc wrote:
/ *已执行检查以确保x为负* /
abs_val = - (unsigned short int)x;

我认为以上是我应该如何安全地获得绝对值
负数的值。我想证实这一点,因为我的Lint软件包
说有一个:
符号丢失(赋值)(int to unsigned short)。
/* already performed check to make sure x is negative */
abs_val = -(unsigned short int)x;

I take it that the above is how I should safely get the absolute value of a negative number. I want to confirm this because my Lint package
says that there is a:
"Loss of sign(assignment)(int to unsigned short)."




我只是想补充一点,如果我将整个事情转换为无符号

short int那么我就不会得到那条消息但是我仍然很好奇为什么Lint

警告我没有演员。



I just wanted to add that if I cast that whole thing to an unsigned
short int then I don''t get that message but I''m still curious why Lint
warns me without the cast.


joshc写道:
...
我想要一个''short int'的绝对值,以避免溢出的危险我正在做以下几点:

short int x = -4; / *想要abs(x)* /
unsigned short int abs_val;

...

/ *已执行检查以确保x为负* /
abs_val = - (unsigned short int)x;

我认为上面是我应该如何安全地获得负数的绝对值。
...
I want the absolute value of a ''short int'' so to avoid the dangers of
overflow I am doing the follow:

short int x = -4; /* want abs(x) */
unsigned short int abs_val;

...

/* already performed check to make sure x is negative */
abs_val = -(unsigned short int)x;

I take it that the above is how I should safely get the absolute value
of a negative number.




让我们看看这个表达式会发生什么。


最初,操作数(''x'')被隐式提升为输入''int''(

值保留)。然后转换为''unsigned short''导致

原始值按照模数算法的规则,即

包围零/ SHORT_MAX + 1限制,即原始负值将
转换为正值''SHORT_MAX + 1 + x''。这个值再一次

隐式提升为''int''(值保留)。现在

一元'' - ''被应用于操作数,导致负值

'' - SHORT_MAX-1-x''''int ''打字。现在,这个最终值被压缩为''unsigned short''类型的
变量。这将导致另一个回绕。

最终值为正' - SHORT_MAX-1-x + SHORT_MAX + 1''。确实

等于''-x''。


似乎工作(我希望我没有错过任何东西),但是如果我是你b $ b你我不会跳过所有这些篮球并使用所有那些

环绕式诡计来取消原始值的否定。我会用这样的方式来做b $ b而不是


/ *已经执行检查以确保x为负* /

abs_val = -x;


这比原始版本更有意义。


-

祝你好运,

Andrey Tarasevich



Let''s see what happens in this expression.

Initially, the operand (''x'') is implicitly promoted to type ''int'' (the
value is preserved). Then the conversion to ''unsigned short'' causes the
original value to wrap around zero/SHORT_MAX+1 limit in accordance with
the rules of modulo arithmetics, i.e. the original negative value turns
into the positive value ''SHORT_MAX+1+x''. This value is once again
implicitly promoted to type ''int'' (the value is preserved). And now the
unary ''-'' is applied to the operand, resulting in a negative value
''-SHORT_MAX-1-x'' of ''int'' type. Now, this final value is squeezed into a
variable of type ''unsigned short''. This will cause another wraparound.
The final value is positive ''-SHORT_MAX-1-x+SHORT_MAX+1''. It is indeed
equal to ''-x''.

It appears to be working (I hope I didn''t miss anything), but if I were
you I wouldn''t jump through all those hoops and employ all that
wraparound trickery to get to the negation of the original value. I
would do it this way instead

/* already performed check to make sure x is negative */
abs_val = -x;

This just makes more sense than the original version.

--
Best regards,
Andrey Tarasevich


joshc写道:
joshc wrote:
joshc写道:
joshc wrote:
/ *已执行检查以确保x为负* /
abs_val = - (unsigned short int)x;

我认为以上是我应该如何安全地获得负数的绝对
/* already performed check to make sure x is negative */
abs_val = -(unsigned short int)x;

I take it that the above is how I should safely get the absolute


。我想证实这一点,因为我的Lint软件包
说有一个:
符号丢失(赋值)(int to unsigned short)。
of a negative number. I want to confirm this because my Lint package
says that there is a:
"Loss of sign(assignment)(int to unsigned short)."



我只是想补充一点,如果我将整个事情投射到一个未签名的简短的int int然后我没有得到那个消息但我仍然很好奇为什么Lint
在没有演员的情况下警告我。



I just wanted to add that if I cast that whole thing to an unsigned
short int then I don''t get that message but I''m still curious why Lint
warns me without the cast.




我不完全理解为什么上面演员会帮你摆脱

警告。但是如果你真的想要使用强制转换,它可能会像这样做更有意义


abs_val =(unsigned short int)-x ;


阅读我之前的消息了解更多详情。


-

祝你好运,

Andrey Tarasevich



I don''t exactly understand why the above cast helps you to get rid of
the warning. But if you really want to use the cast, it would probably
make more sense to do it like this

abs_val = (unsigned short int) -x;

Read my previous message for more details.

--
Best regards,
Andrey Tarasevich


这篇关于一元否定运算符问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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