c语言中移位出现的问题,困惑

klangkang 2010-09-27 06:02:30
定义了一个32位的变量。
sourAddr = (msg->identifier&0x0001fe00)>>9;
uart0_putc(sourAddr);//打印的数值时正确的
sourAddr <<= 8;
sourAddr |= (msg->identifier&0x01fe0000)>>17;
uart0_putc(sourAddr);//打印的数值也是正确的
sourAddr <<= 9;
下面见证奇迹:
uart0_putc((uint8_t)sourAddr );//打印出来的是0x00
uart0_putc((uint8_t)(sourAddr>>8 ));//打印出来的是0x00
uart0_putc((uint8_t)(sourAddr>>16) );//打印出来的是0x00
uart0_putc((uint8_t)(sourAddr>>24) );//打印出来的是0x00

为毛啊??困惑的要挂了
...全文
96 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
klangkang 2010-09-30
  • 打赏
  • 举报
回复
检查结果是我定义在定义变量的时候类型定义错了!不是第一次犯这个错误了。结贴
klangkang 2010-09-27
  • 打赏
  • 举报
回复
我已经用强制转换了。我想不会存在这个问题的
pengzhixi 2010-09-27
  • 打赏
  • 举报
回复
If the value of the right operand is negative or is
greater than or equal to the width of the promoted left operand, the behavior is undefined
justkk 2010-09-27
  • 打赏
  • 举报
回复
uint8_t只有一个字节,是这个关系不?

69,371

社区成员

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

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