代码问题 [英] Code problem

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

问题描述

我把它发布到comp.std.c,但也可能会感兴趣:

考虑一下:


extern void abort(void);

int main(void)

{

unsigned long long xx;

unsigned long long * x =(unsigned long long *)& xx;


* x = -3;

* x = * x * * x;

if(* x!= 9)

abort();

return(0);

}


lcc-win解释

* x = -3;



* x = 4294967293 ;

因为x指向一个UNSIGNED多长。

我将32位整数-3转换为无符号整数

然后我投了结果是一个无符号长的长。


显然gcc不同意。


我在某处做错了吗?


我应该先投入很长一段时间,然后再签订一份未签名的长期




感谢您的帮助。


-

jacob navia

jacob at jacob p oint remcomp point fr

logiciels / informatique
http://www.cs.virginia.edu/~lcc-win32

I posted this to comp.std.c, but may be of interest here too:

Consider this:

extern void abort(void);
int main (void)
{
unsigned long long xx;
unsigned long long *x = (unsigned long long *) &xx;

*x = -3;
*x = *x * *x;
if (*x != 9)
abort ();
return(0);
}

lcc-win interprets
*x = -3;
as
*x = 4294967293;
since x points to an UNSIGNED long long.
I cast the 32 bit integer -3 into an unsigned integer
then I cast the result to an unsigned long long.

Apparently gcc disagrees.

Am I doing something wrong somewhere?

I should first cast into a long long THEN into an unsigned
long long?

Thanks for your help.

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32

推荐答案

jacob navia< ja ** *@nospam.comwrites:
jacob navia <ja***@nospam.comwrites:

int main(void)

{

unsigned long long xx;

unsigned long long * x =(unsigned long long *)& xx;
int main (void)
{
unsigned long long xx;
unsigned long long *x = (unsigned long long *) &xx;



为什么演员?它应该是不必要的。

Why the cast? It should be unnecessary.


* x = -3;

* x = * x * * x;

if(* x!= 9)

abort();

return(0);

}


lcc-win解释

* x = -3;



* x = 4294967293;

,因为x指向UNSIGNED多长。
*x = -3;
*x = *x * *x;
if (*x != 9)
abort ();
return(0);
}

lcc-win interprets
*x = -3;
as
*x = 4294967293;
since x points to an UNSIGNED long long.



unsigned long long的大小必须至少为64位。

18446744073709551613是此处* x的最小正确值。 />

unsigned long long has to be at least 64 bits in size.
18446744073709551613 is the minimum correct value for *x here.


我将32位整数-3转换为无符号整数

然后我将结果转换为unsigned long long。
I cast the 32 bit integer -3 into an unsigned integer
then I cast the result to an unsigned long long.



我在上面的程序中没有看到任何转换为​​unsigned int。

-

Ben Pfaff
http://benpfaff.org


jacob navia说:
jacob navia said:

我把它发布到comp.std.c,但也可能会感兴趣:


考虑一下:


extern void abort(void);

int main(void)

{

unsigned long long xx;

unsigned long long * x =(unsigned long long *)& xx;
I posted this to comp.std.c, but may be of interest here too:

Consider this:

extern void abort(void);
int main (void)
{
unsigned long long xx;
unsigned long long *x = (unsigned long long *) &xx;



删除不必要的演员:


unsigned long long * x =& xx;

Remove the unnecessary cast:

unsigned long long *x = &xx;


>

* x = -3;
>
*x = -3;



见6.2.5(9)。

See 6.2.5(9).


* x = * x * * x;

if(* x!= 9)

abort();
*x = *x * *x;
if (*x != 9)
abort ();



此时* x几乎不可能是9。

It is deeply unlikely that *x will be 9 at this point.


return(0) ;

}


lcc-win解释

* x = -3;



* x = 4294967293;

因为x指向UNSIGNED long long。
return(0);
}

lcc-win interprets
*x = -3;
as
*x = 4294967293;
since x points to an UNSIGNED long long.



这是lcc-win中的一个错误。 * x必须具有ULLONG_MAX - 2的值,并且因为

ULLONG_MAX必须至少为18446744073709551615,* x必须至少为

18446744073709551613。

That''s a bug in lcc-win. *x must have the value ULLONG_MAX - 2, and since
ULLONG_MAX must be at least 18446744073709551615, *x must be at least
18446744073709551613.


我将32位整数-3转换为无符号整数

然后我将结果转换为unsigned long long。
I cast the 32 bit integer -3 into an unsigned integer
then I cast the result to an unsigned long long.



为什么?

Why?


显然gcc不同意。


我在某处做错了吗?
Apparently gcc disagrees.

Am I doing something wrong somewhere?



是的。如果你的文章准确描述了lcc-win'的行为,那么你的错误在于使用不合格的编译器。

Yes. If your article is an accurate description of lcc-win''s behaviour,
your mistake is in using a non-conforming compiler.


我应该首先投入很长一段时间,然后再投入一个未签约的长期?

I should first cast into a long long THEN into an unsigned
long long?



不,几乎可以肯定没有必要投出任何东西。第一个

步骤是确定您要解决的问题,这远远不是明确的。如果你想知道-3乘-3的结果,为什么首先使用

无符号类型?


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

No, there is almost certainly no need to cast anything at all. The first
step is to identify the problem you are trying to solve, which is far from
clear. If you want to know the result of multiplying -3 by -3, why use an
unsigned type in the first place?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


jacob navia写道:
jacob navia wrote:

我把它发布到comp.std.c,但也可能感兴趣:


考虑一下:


extern void abort(void);

int main(void)

{

unsigned long long xx;

unsigned long long * x =(unsigned long long *)& xx ;


* x = -3;

* x = * x * * x;

if(* x!= 9)

abort();

返回(0);

}


lcc-赢得解释

* x = -3;

as

* x = 4294967293;

因为x指向一个UNSIGNED漫长的。
I posted this to comp.std.c, but may be of interest here too:

Consider this:

extern void abort(void);
int main (void)
{
unsigned long long xx;
unsigned long long *x = (unsigned long long *) &xx;

*x = -3;
*x = *x * *x;
if (*x != 9)
abort ();
return(0);
}

lcc-win interprets
*x = -3;
as
*x = 4294967293;
since x points to an UNSIGNED long long.



ULLONG_MAX的最小值是18446744073709551615,所以你

应该得到一个至少18446744073709551613的值

The minimum value for ULLONG_MAX is 18446744073709551615, so you
should be getting a value of at least 18446744073709551613


我将32位整数-3转换为无符号整数

然后我将结果转换为unsigned long long。
I cast the 32 bit integer -3 into an unsigned integer
then I cast the result to an unsigned long long.



你为什么这样做?

Why did you do that?


显然gcc不同意。


我在某处做错了吗?
Apparently gcc disagrees.

Am I doing something wrong somewhere?



是的。

Yes.


我应该首先投入很长的时间然后进入一个未签名的

多长?
I should first cast into a long long THEN into an unsigned
long long?



为什么这么认为?你为什么要使用中介?


你应该将-3直接转换成无符号的long long。你应该

既不绕过unsigned int,也不绕过long long。

结果应为ULLONG_MAX + 1-3。

Why do you think that? Why are you using intermediaries?

You should be converting -3 directly to unsigned long long. You should
neither detour through unsigned int, nor detour through long long. The
result should be ULLONG_MAX+1-3.


这篇关于代码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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