printf("%d",-1<<10);
I marked its answer as -1024 by the logic 2^10*-1
我甚至在gcc上运行它并且它给了我o / p为-1024(当我回到家时。)
答案 0 :(得分:3)
规则没有改变。它在技术上仍未定义。
引用C标准(n1548第6.5.7节第4段):
E1的结果&lt;&lt; E2是E1左移E2位位置;空出的位被填充 零。如果E1具有无符号类型,则结果的值为E1×2 ^ E2,比结果类型中可表示的最大值减少一个模数。如果E1具有带符号类型和非负值,并且在结果类型中可以表示E1×2 ^ E2,则表示 结果价值; 否则,行为未定。
它清楚地表明如果E1
未签名或使用非负值签名,则行为未定义。
答案 1 :(得分:2)
在移动操作员>>
或<<
中:
操作数应为integral
类型或受integral promotion
约束。
如果右操作数为负或大于左操作数中的位,则为Undefined behaviour
如果左操作数为>>
为负数,那么implementation defiend
和<<
undefined behaviour
引自K&amp; R附录-A
The shift operators << and >> group left-to-right. For both operators,
each operand must be integral, and is subject to integral the promotions.
The type of the result is that of the promoted left operand.
The result is undefined if the right operand is negative,
or greater than or equal to the number of bits in the left expression's type.
shift-expression:
additive-expression
shift-expression << additive-expression
shift-expression >> additive-expression
The value of E1<<E2 is E1 (interpreted as a bit pattern) left-shifted E2 bits;
in the absence of overflow, this is equivalent to multiplication by 2. The value
of E1>>E2 is E1 right-shifted E2 bit positions. The right shift is equivalent to division
by 2. if E1 is unsigned or it has a non-negative value; otherwise the result is
implementation-defined.
答案 2 :(得分:-2)
也许你认为正确的操作数是否定的?
http://msdn.microsoft.com/en-us/library/336xbhcz(v=vs.80).aspx