由0位置左负值的转变? [英] Left shift of negative values by 0 positions?

查看:638
本文介绍了由0位置左负值的转变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C,负值左移是不确定的行为。我遇到了Intel的ICC,其中除去违规code编译两个库。同样的code下铛,科莫,GCC和MSVC的罚款。

In C, a left shift of a negative value is undefined behavior. I've encountered two libraries compiled with Intel's ICC where the offending code was removed. The same code was fine under Clang, Comeau, GCC and MSVC.

是否标准做出左移负值0的地方没有提到?难道还没有定义?

Does the standard make any mention of left shifting a negative value 0 places? Is it also undefined?

(我很好奇的细节是一个0大小的转变,这是所有在实践中没有改变。所以我想知道如果语言含糊不清,使得0大小的左移可以被允许)。

(The detail I'm curious about is a 0-sized shift, which is no shift at all in practice. So I'm wondering if the language is vague such that a 0-sized left shift may be allowed).

推荐答案

从的 C99与技术勘误TC1,TC2,TC3和包括:

6.5.7按位移位运算符

6.5.7 Bitwise shift operators

[...]

整数促销活动在每个操作数执行。的结果的类型是
  该促进左操作数。如果右操作数的值是负的或者是
  大于或等于促进左操作数的宽度,其行为是不确定的。结果
  E1 1所述的结果;&下; E2是E1左移E2位位置;腾空位填充
  零。如果E1有一个无符号类型,则结果的值是E1×2 E2 ,降低的模
  人比在结果类型的最大值再presentable多。 如果E1有一个签名
  类型和非负价值,E1×2 E2 重新presentable的结果类型,然后就是
  所得到的值;否则,行为是不确定的。

The integer promotions are performed on each of the operands. The type of the result is that of the promoted left operand. If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.
The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are filled with zeros. If E1 has an unsigned type, the value of the result is E1 × 2E2, reduced modulo one more than the maximum value representable in the result type. If E1 has a signed type and nonnegative value, and E1 × 2E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined.

所以,总是不确定的。

这篇关于由0位置左负值的转变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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