c语言中右移问题

swordtan 2012-09-27 05:19:58

/*
* main.c
*
* Created on: 2012-9-27
*/

#include <stdio.h>

int main(){
int a = 0x80000000, b, c;
b = (a & 0xFFFFFFFF) >> 1;
printf("%d\n", b);
c = (a | 0x0) >> 1;
printf("%d\n", c);
return 0;
}


输出结果:
1073741824
-1073741824


为什么 (a & 0xFFFFFFFF) >> 1 是逻辑右移
而(a | 0x0) >> 1; 是算数右移呢?
...全文
322 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2012-09-28
  • 打赏
  • 举报
回复
#include <stdio.h>
unsigned short int ui;
signed short int si;
int main() {
ui=(unsigned short int)0x8000u;
si=( signed short int)0x8000;
printf("ui=%u\n",ui);
printf("si=%d\n",si);
ui=ui>>1;
si=si>>1;
printf("ui=%u\n",ui);
printf("si=%d\n",si);

printf("--------------\n");
ui=(unsigned short int)0x8000u;
si=( signed short int)0x8000;
printf("ui=%u\n",ui);
printf("si=%d\n",si);
ui=(( signed short int)ui)>>1;
si=((unsigned short int)si)>>1;
printf("ui=%u\n",ui);
printf("si=%d\n",si);

return 0;
}
//ui=32768
//si=-32768
//ui=16384
//si=-16384
//--------------
//ui=32768
//si=-32768
//ui=49152
//si=16384
mymtom 2012-09-28
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]
这是哪里的说明?
[/Quote]
C89
[url=http://www.bsb.me.uk/ansi-c/ansi-c-one-file.html][url]
ouPuso 2012-09-28
  • 打赏
  • 举报
回复
0x00UL
AnYidan 2012-09-28
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

是0xFFFFFFFF的类型超过了有符号的范围,使得数据类型提升为无符号的结果
lz可以这样测试一下:
printf("%#x, %#x\n",(a&(-1))>>1, (a&0xffffffff)>>1);
[/Quote]

If an int can represent all the values of the original type, then the value is converted to int; otherwise the value is converted to unsigned int. This process is called integral promotion.
swordtan 2012-09-27
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

是0xFFFFFFFF的类型超过了有符号的范围,使得数据类型提升为无符号的结果
lz可以这样测试一下:
printf("%#x, %#x\n",(a&(-1))>>1, (a&0xffffffff)>>1);
[/Quote]

你的意思是,文字整型常量是有类型的,如:
0x0 ~ 0x7FFFFFFF int类型
0x80000000 ~ 0xFFFFFFFF 是unsigned int类型的,
对吧?
AndyZhang 2012-09-27
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed type and a nonnegative value, the value of the result is the integral part of the quotient……
[/Quote]
这是哪里的说明?
ouPuso 2012-09-27
  • 打赏
  • 举报
回复
是0xFFFFFFFF的类型超过了有符号的范围,使得数据类型提升为无符号的结果
lz可以这样测试一下:
printf("%#x, %#x\n",(a&(-1))>>1, (a&0xffffffff)>>1);
mujiok2003 2012-09-27
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed type and a nonnegative value, the value of the result is the integral part of the quotient……
[/Quote]

++
hu7324829 2012-09-27
  • 打赏
  • 举报
回复
算术位移 只是针对 有符号数有不同.


c = (a | 0x0u) >> 1;


如果提升a成无符号类型, 将会进行逻辑右移
mymtom 2012-09-27
  • 打赏
  • 举报
回复
The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed type and a nonnegative value, the value of the result is the integral part of the quotient of E1 divided by the quantity, 2 raised to the power E2. [B]If E1 has a signed type and a negative value, the resulting value is implementation-defined[/B].
图灵狗 2012-09-27
  • 打赏
  • 举报
回复
参与位运算时,最好用无符号类型:

/*
* main.c
*
* Created on: 2012-9-27
*/

#include <stdio.h>

int main(){
unsigned int a = 0x80000000, b, c;
b = (a & 0xFFFFFFFF) >> 1;
printf("%d\n", b);
c = (a | 0x0) >> 1;
printf("%d\n", c);
return 0;
}

冷月清晖 2012-09-27
  • 打赏
  • 举报
回复
因为(a | 0x0) >> 1;的效果相当于除以2。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧