左右移位负整数是否定义行为? [英] Is left and right shifting negative integers defined behavior?

查看:547
本文介绍了左右移位负整数是否定义行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,右移负号类型取决于实现,但如果我执行左移?例如:

I know, right shifting a negative signed type depends on the implementation, but what if I perform a left shift? For example:

int i = -1;
i << 1;

这是否定义良好?

如果E1有一个有符号类型和非负值,我认为该标准没有说明有符号类型的负值。

I think the standard doesn't say about negative value with signed type


×2 E2
在结果类型中可表示,那么它是结果值;
否则,行为是未定义的。

if E1 has a signed type and non-negative value, and E1 × 2E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined.

它只澄清如果结果不能在signed类型中表示,行为未定义。

It only clarifies that if the result isn't representable in signed type then the behavior is undefined.

推荐答案

您没有正确读取该句子。标准定义它if:左操作数具有有符号类型非负值,结果是可表示的(以前在相同的段落中为无符号类型定义)。在所有其他情况下(请注意在该句子中使用分号 ),即,这些条件未验证,行为未定义。

You're not reading that sentence correctly. The standard defines it if: the left operand has a signed type and a non-negative value and the result is representable (and previously in the same paragraph defines it for unsigned types). In all other cases (notice the use of the semicolon in that sentence), i.e, if any of these conditions isn't verified, the behaviour is undefined.

这篇关于左右移位负整数是否定义行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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