右移操作员 [英] shift right operator

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

问题描述

main()

{

int a = 100;

a = a>> 32;

printf("%d",a);

}


它打印a"只有100 ....但我期待a = 0 .....

有人可以告诉我原因吗?


bye

Deepak \

解决方案

DeepaK KC写道:

main()
{
int a = 100;
a = a>> 32;
printf("%d",a);
}
<它打印a只有100 ....但我期待a = 0 .....
有人可以告诉我原因吗?




它''表现未定义的行为。 (例如,有些机器

只有5位移位数。)


如果你打开编译器警告怎么办?

-

Chris" electric hedgehog" Dollin


DeepaK KC写道:

main()
{
int a = 100;
a = a>> 32;
printf("%d",a);
}
它打印a只有100 ....但我期待a = 0 .....
有人可以告诉我原因吗?

再见
Deepak \



你的程序的固定版本:

#include< stdio.h>

int main(void)

{

int a = 100;

a = a>> 32;

printf("%d \\ \\ n",a);

返回0;

}


temp(558)


gcc -Wall -o foo foo.c

foo.c:在函数`main''中:

foo.c:6:警告:右移count> =类型的宽度

temp(559)


main()
{
int a =100;
a = a>>32;
printf(" %d", a);
}

it prints "a" as 100 only....but I am expecting a = 0.....
can some one tell me the reason?

bye
Deepak\

解决方案

DeepaK K C wrote:

main()
{
int a =100;
a = a>>32;
printf(" %d", a);
}

it prints "a" as 100 only....but I am expecting a = 0.....
can some one tell me the reason?



It''s a manifestation of undefined behaviour. (Some machines
only have a 5-bit shift count, for example.)

What happens if you turn your compiler warnings up?
--
Chris "electric hedgehog" Dollin


DeepaK K C wrote:

main()
{
int a =100;
a = a>>32;
printf(" %d", a);
}

it prints "a" as 100 only....but I am expecting a = 0.....
can some one tell me the reason?

bye
Deepak\



Fixed version of your program:
#include <stdio.h>
int main(void)
{
int a = 100;
a = a>>32;
printf(" %d\n", a);
return 0;
}

temp(558)


gcc -Wall -o foo foo.c
foo.c: In function `main'':
foo.c:6: warning: right shift count >= width of type
temp(559)


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

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