加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 反复移动“x << = 1”后,值是否为0?

发布时间:2020-12-16 05:19:36 所属栏目:百科 来源:网络整理
导读:我想知道x在以下程序中是否达到零. 请考虑: int main (){ int x = 1; while (x) { x = 1; } return 0;} 该程序的预期行为是否正常退出或永久循环? 解决方法 既不是(或两者),当x溢出时,它以未定义的行为运行. C99规格6.5.7节说: The result of E1 E2 is E1
我想知道x在以下程序中是否达到零.

请考虑:

int main ()
{
    int x = 1;
    while (x)
      {
        x <<= 1;
      }
    return 0;
}

该程序的预期行为是否正常退出或永久循环?

解决方法

既不是(或两者),当x溢出时,它以未定义的行为运行.

C99规格6.5.7节说:

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读