结果为1<< 32 [英] The result of 1 << 32
问题描述
全部,
这是一个我无法理解的问题。
int main()
{
int i = 32;
printf("%p%p\ n",1<< i,1<< 32); < br $>
}
结果是:
0x1 0x0
(" int" ;我的机器上的类型是32位)
为什么1<<< i和1<< 32得到不同的结果?我原以为
他们两个都会得到0x0。
-
BY
2005-10-29,mrby< bi ****** @ gmail.com>写道:全部,
这是一个我无法理解的问题。
int main()
{
int i = 32;
printf("%p%p \ n",<<< i,1<<<<< 32);
结果是:
0x1 0x0
(我的机器上的int类型是32位)
为什么1<<< i和1<< 32得到不同的结果?我原以为
他们两个都会得到0x0。
一个理论:1<< 32在编译时计算[和编译器
本身就足够智能了,而在运行时计算的1<< i值是
- 现在,如果你的处理器的LSH指令是
只接受一个5位的立即操作数。
根据标准,移动一个大于或等于
的数字等于字段宽度是未定义。
A.6.2未定义的行为
*表达式被移动一个负数或一个数量
大于或等于表达式的位宽
被移位(§3.3.7)。
你的解释是有道理的!我很感激。
-
BY
周六, 2005年10月29日15:41:20 +0000,Jordan Abel写道:2005-10-29,mrby< bi ****** @ gmail.com>写道:全部,
这是一个我无法理解的问题。
int main()
{
int i = 32;
printf("%p%p \ n",<<< i,1<<<<< 32);
结果是:
0x1 0x0
(我的机器上的int类型是32位)
为什么1<<< i和1<< 32得到不同的结果?我原以为
他们两个都会得到0x0。
一个理论:1<< 32在编译时计算[和编译器
本身是聪明到足以做到这一点]而1<< i value is
所以你假设环绕,这是合理的但不是唯一的
可能性。正如你从标准中引用的那样,这是未定义的行为
- 所有投注都已关闭,任何结果都是可能的。
要添加,使用的printf格式说明符是指针的格式说明符。它是
应该是%d。
[...]
-
http://members.dodo.com.au/~netocrat
All,
Here is one question that I can not understand.
int main()
{
int i = 32;
printf("%p %p\n", 1<<i, 1<<32);
}
The result is:
0x1 0x0
("int" type on my machine is 32-bit)
Why 1<<i and 1<<32 get different result? I had thought
both of them would get 0x0.
--
B.Y.
On 2005-10-29, mrby <bi******@gmail.com> wrote:All,
Here is one question that I can not understand.
int main()
{
int i = 32;
printf("%p %p\n", 1<<i, 1<<32);
}
The result is:
0x1 0x0
("int" type on my machine is 32-bit)
Why 1<<i and 1<<32 get different result? I had thought
both of them would get 0x0.
A theory: the 1<<32 is calculated at compile time [and the compiler
itself is smart enough to do this] while the 1<<i value is
calculated at runtime - now, if your processor''s LSH instruction
only accepts a 5-bit immediate operand.
According to the standard, shifting by a number greater than or
equal to the field width is undefined.
A.6.2 Undefined Behavior
* An expression is shifted by a negative number or by an amount
greater than or equal to the width in bits of the expression
being shifted (§3.3.7).
You explanation makes sense! I do appreciate it.
--
B.Y.
On Sat, 29 Oct 2005 15:41:20 +0000, Jordan Abel wrote:On 2005-10-29, mrby <bi******@gmail.com> wrote:All,
Here is one question that I can not understand.
int main()
{
int i = 32;
printf("%p %p\n", 1<<i, 1<<32);
}
The result is:
0x1 0x0
("int" type on my machine is 32-bit)
Why 1<<i and 1<<32 get different result? I had thought
both of them would get 0x0.
A theory: the 1<<32 is calculated at compile time [and the compiler
itself is smart enough to do this] while the 1<<i value is
So you''re assuming wrap-around, which is reasonable but not the only
possibility. As you quoted from the standard, this is undefined behaviour
- all bets are off and any result''s possible.
To add to that, the printf format specifier used is that of a pointer. It
should be %d.
[...]
--
http://members.dodo.com.au/~netocrat
这篇关于结果为1<< 32的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!