本文介绍了当左操作数未定义行为为负的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天就回来我给微软GD在线考试实习那里。我一直研究了负数的左移是不确定的行为,但该文件几乎7个问题出30相关的转向运营商和大约5个问题分别疗法这涉及到转向负数离开,他们别无选择,说未定义行为。我很震惊地看到这一点。所以,我的问题是,有这个C标准改变了吗?这是目前定义的?
样本问题:

few days back I gave Microsoft GD online exam for internship there. I have always studied that the left shift of negative number is an undefined behavior but that paper had almost 7 questions out of 30 related to shift operators and approx 5 questions were ther which involved shifting negative numbers to left and they had no option saying "undefined behavior". I was shocked to see that . So, my question is that has this C standard changed ? Is this defined now ?sample question :

printf("%d",-1<<10);

I marked its answer as -1024 by the logic 2^10*-1

我甚至跑这对gcc和它给了我O / P为-1024 NLY(当我回到了家。)

I even ran this on gcc and it gave me o/p as -1024 nly (when I returned home.)

推荐答案

规则并没有改变。它仍然在技术上不确定的。

The rules haven't changed. It's still technically undefined.

从C标准引用(第6.5.7条第4款n1548的):

Quoting from the C standard (Section 6.5.7, paragraph 4, of n1548):

E1 1所述的结果;&下; E2是E1左移E2位位置;腾空位音响与LLED
  零。如果E1有一个无符号类型,则结果的值是E1×2 ^ E2,减小模数比在结果类型的最大值重新presentable一个。如果E1有符号类型和非负价值,在E1结果类型×2 ^ E2重新presentable,然后就是
  所得到的值; ,否则,其行为定义理解过程网络。

它清楚地说,如果 E1 不签名或与非负值签署,该行为是不确定的。

It clearly says if E1 is not unsigned or signed with nonnegative value, the behavior is undefined.

这篇关于当左操作数未定义行为为负的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 00:33