更多的转移问题 [英] More bit shifting issues

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

问题描述



我似乎对比特移位有更多奇怪的问题。似乎

以下代码在gcc下没有做任何事情(即它返回-1为

两个结果)。谁知道为什么?是另一种语言定义还是

CPU问题?


main()

{

printf ("%d \ n",(int)0xFFFFFFFF> 1);

printf("%d \ n",(int)-1> 1);

}


B2003


I seem to be having yet more wierd issue with bit shifting. It seems
the following code doesnt do anything under gcc (ie it returns -1 as
both results). Anyone know why? Is it another language definition or
CPU issue?

main()
{
printf("%d\n",(int)0xFFFFFFFF >1);
printf("%d\n",(int)-1 >1);
}

B2003

推荐答案

Boltar< bo ***** ***@yahoo.co.ukwrites:
Boltar <bo********@yahoo.co.ukwrites:

我似乎对比特移位有更多奇怪的问题。似乎

以下代码在gcc下没有做任何事情(即它返回-1为

两个结果)。谁知道为什么?是另一种语言定义还是

CPU问题?


main()

{

printf ("%d \ n",(int)0xFFFFFFFF> 1);

printf("%d \ n",(int)-1> 1);

}


B2003
I seem to be having yet more wierd issue with bit shifting. It seems
the following code doesnt do anything under gcc (ie it returns -1 as
both results). Anyone know why? Is it another language definition or
CPU issue?

main()
{
printf("%d\n",(int)0xFFFFFFFF >1);
printf("%d\n",(int)-1 >1);
}

B2003



尝试使用无符号值并报告回来......

Try using unsigned values and report back ...


Boltar< bo ******** @ yahoo.co.ukwrote:
Boltar <bo********@yahoo.co.ukwrote:

printf(" %d \ n",(int)0xFFFFFFFF> 1);

printf("%d \ n",(int)-1> 1);
printf("%d\n",(int)0xFFFFFFFF >1);
printf("%d\n",(int)-1 >1);



右移一个负值会产生一个实现定义的

值。作为第一个猜测,你的实现算术算术正确你想要一个逻辑的算术。两个结果都是允许的。


Richard

Right-shifting a negative value results in an implementation-defined
value. As a first guess, your implementation does an arithmetic right
shift where you expected a logical one. Both results are allowed.

Richard


Boltar写道:
Boltar wrote:

>

我似乎有更多奇怪的位移问题。似乎

以下代码在gcc下没有做任何事情(即它返回-1为

两个结果)。谁知道为什么?是另一种语言定义还是

CPU问题?


main()

{

printf ("%d \ n",(int)0xFFFFFFFF> 1);

printf("%d \ n",(int)-1> 1);

}
>
I seem to be having yet more wierd issue with bit shifting. It seems
the following code doesnt do anything under gcc (ie it returns -1 as
both results). Anyone know why? Is it another language definition or
CPU issue?

main()
{
printf("%d\n",(int)0xFFFFFFFF >1);
printf("%d\n",(int)-1 >1);
}



负值的Shift-right是实现定义的;你很可能获得算术右移或逻辑右移之一的
。 (有些机器

只有其中一种,而且必须合成另一种机器,这对于b $ b来说是非常低效的。)


明智的做法是将按位操作限制为无符号类型,因为

这些不能用狡猾的符号来削减你的令人讨厌的尖锐

边缘。


-

"整个装置看起来已经放好了/老鹰杰克/

在一起狂热地狂热地狂热地
>
细心的技术人员可以鼓起勇气。


Hewlett-Packard Limited注册号:

注册办公室:Cain Road,Bracknell,Berks RG12 1HN 690597英格兰

Shift-right of negative values is implementation-defined; you''re likely
to get one of arithmetic right shift or logical right shift. (Some machines
have/had only one of these and would have to synthesise the other, which
would be Woefully Inefficient.)

It''s wise to restrict ones bitwise operations to unsigned types, since
these can''t have tricksy sign bits to cut you with their nasty sharp
edges.

--
"The whole apparatus had the look of having been put /Jack of Eagles/
together with the most frantic haste a fanatically
careful technician could muster."

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England


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

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