这个输出的解释 [英] explanation for this output

查看:77
本文介绍了这个输出的解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码如何运作。


main(){


int a = 38,b = 13;

unsigned long long c;


c = a *(1<< b)* 32000;


printf(" ;%llu",c);

}


此代码的输出不是9961472000,而是1371537408.


如何将此转换为此数字?是因为处理器中的寄存器



解决方案

2007年6月20日,deepak< ; de ********* @ gmail.comwrote:


以下代码如何工作。


main(){


int a = 38,b = 13;

unsigned long long c;


c = a *(1 <<<< b)* 32000;


printf("%llu",c);

}


此代码的输出不是9961472000,而是1371537408.


如何转换为此数字?是因为处理器中的
寄存器?



这里有一个提示:


9961472000 = 1001010001110000000000000000000000

1371537408 = 01010001110000000000000000000000


试试3200LL。


戴夫


-

David Tiktin

tiktin [at] advancedrelay [dot] com


文章< 11 *********** ***********@x35g2000prf.googlegroups .com> ;,

deepak< de ********* @ gmail.comwrote:
< blockquote class =post_quotes>
>以下代码如何工作。


> main(){


int a = 38 ,b = 13;

unsigned long long c;


c = a *(1<< b)* 32000;


printf("%llu",c);
}


>此代码的输出不是9961472000,而是1371537408.


>这是如何转换为此数字的?是因为处理器中的寄存器吗?



将输出分配给long long变量

的事实并不意味着表达式将以long long计算。

相反,因为所涉及的变量都是int,所以使用int算法将值计算为
,因为

算术溢出会遇到UB int [在大多数系统上],无论是什么

出来都会扩大到长长的c。


尝试


c =(long long)a *(1LL<< b)* 32000LL;


(可能会有更紧凑的表达式与

相同的结果,但上面并不要求代码

维护者考虑宽度促销的神秘细节。

-

有些想法是错的,只有一个非常聪明的人才能相信他们。 - George Orwell


在文章< ; XN ****** ****************************@216.196.97.1 36> ;,

David Tiktin< dt ** ***@nospam.totally-bogus.comwrote:


> 2007年6月20日,deepak< de ********* @ gmail .comwrote:


> int a = 38,b = 13;


> c = a *(1 << b)* 32000;


>尝试使用3200LL。



否,那么a *(1<< b)仍将被评估为int;

无限制宽度评估38 * 2 ** 13是311296

超过int的保证宽度(只需要

go高达32767)。你提出的建议可能会发生在某个特定系统上的工作,如果int至少是19个值

位宽(加号信息),但它只会持续存在

一个难以找到的形式的原始错误。

-

如果你撒谎到编译器,它将报复。 - Henry Spencer


How the following code is working.

main() {

int a = 38, b = 13;
unsigned long long c;

c = a * (1<<b) * 32000;

printf("%llu", c);
}

The output of this code is not 9961472000 and it is 1371537408.

How this converting in to this number? Is it because of the registers
in the processor?

解决方案

On 20 Jun 2007, deepak <de*********@gmail.comwrote:

How the following code is working.

main() {

int a = 38, b = 13;
unsigned long long c;

c = a * (1<<b) * 32000;

printf("%llu", c);
}

The output of this code is not 9961472000 and it is 1371537408.

How this converting in to this number? Is it because of the
registers in the processor?

Here''s a hint:

9961472000 = 1001010001110000000000000000000000
1371537408 = 01010001110000000000000000000000

Try it with 3200LL.

Dave

--
D.a.v.i.d T.i.k.t.i.n
t.i.k.t.i.n [at] a.d.v.a.n.c.e.d.r.e.l.a.y [dot] c.o.m


In article <11**********************@x35g2000prf.googlegroups .com>,
deepak <de*********@gmail.comwrote:

>How the following code is working.

>main() {

int a = 38, b = 13;
unsigned long long c;

c = a * (1<<b) * 32000;

printf("%llu", c);
}

>The output of this code is not 9961472000 and it is 1371537408.

>How this converting in to this number? Is it because of the registers
in the processor?

The fact that you assign the output to a long long variable
does not mean that the expression will be evaluated in long long.
Instead, because the variables inolved are all int, the value will
be evaluated using int arithmetic, will encounter UB because of
the arithmetic overflow of int [on most systems], and whatever
comes out will be widened to the long long c.

Try

c = (long long) a * (1LL<<b) * 32000LL;

(probably there''s a slightly more compact expression with the
same results, but the above doesn''t require that the code
maintainers think about the arcane details of width promotions.
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell


In article <Xn**********************************@216.196.97.1 36>,
David Tiktin <dt*****@nospam.totally-bogus.comwrote:

>On 20 Jun 2007, deepak <de*********@gmail.comwrote:

> int a = 38, b = 13;

> c = a * (1<<b) * 32000;

>Try it with 3200LL.

No, then a * (1<<b) would still be evaluated as int; the
unconstrained width evaluation of 38 * 2**13 is 311296
which exceeds the guaranteed width of int (which only has to
go as high as 32767). What you propose might happen to
work on that particular system, if int is at least 19 value
bits wide (plus sign information), but it would just be perpetuating
the original mistake in a harder-to-find form.
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer


这篇关于这个输出的解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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