[开发工具]

询KEIL中按位取反~操作符问题

[复制链接]
8242|8
手机看帖
扫描二维码
随时随地手机跟帖
xsq5360|  楼主 | 2010-5-13 20:19 | 显示全部楼层 |阅读模式
定义变量如下:
u8 datacode = 0x00;
u8 Rdatacode = 0xFF;
在KEIL uVision3中用到如下代码if(~Rdatacode != datacode),单步调试该表达式结果竟然为真?但若用表达式Rdatacode = ~Rdatacode,则运行后Rdatacode得到正常结果0x00。
实在不解,特此请教!
pentral2010| | 2010-5-13 20:25 | 显示全部楼层
应该是这样吧
Rdatacode &=~Rdatacode;

使用特权

评论回复
xsq5360|  楼主 | 2010-5-13 21:06 | 显示全部楼层
Rdatacode = ~Rdatacode就是把Rdatacode按位取反后在赋值给Rdatacode,这个没错,运行结果也是对的

使用特权

评论回复
voidx| | 2010-5-13 21:43 | 显示全部楼层
本帖最后由 voidx 于 2010-5-14 10:15 编辑

如果int是32位,

~0xFF结果不是0X00,而是是0XFFFFFF00,

0XFFFFFF00!=0x00,
当然为真。

使用特权

评论回复
mcu5i51| | 2010-5-14 09:34 | 显示全部楼层
试过了,正常 无标题.jpg

使用特权

评论回复
xsq5360|  楼主 | 2010-5-14 11:08 | 显示全部楼层
本帖最后由 xsq5360 于 2010-5-14 11:18 编辑

5# mcu5i51
5.GIF
为何我的偏偏不行?版本为uVision3 V3.80,芯片是STM32F103VCH

使用特权

评论回复
voidx| | 2010-5-14 11:25 | 显示全部楼层
本帖最后由 voidx 于 2010-5-14 11:32 编辑

已经说的很明白了。

5楼用的肯定是KEIL C51。

为什么KEIL C51能行?而 KEIL MDK却不行呢?

原因是KEIL C51很不符合C标准的C编译器。
而KEIL MDK则是相当遵守C标准的C编译器。

使用特权

评论回复
xsq5360|  楼主 | 2010-5-14 11:26 | 显示全部楼层
6.GIF
确如上官所言,默认int为32位,加入类型转换后逻辑才正确。

使用特权

评论回复
voidx| | 2010-5-14 11:39 | 显示全部楼层
C语言标准规定:

The result of the ~ operator is the bitwise complement of its (promoted) operand (that is,
each bit in the result is set if and only if the corresponding bit in the converted operand is
not set).The integer promotions are performed on the operand, and the result has the
promoted type.If the promoted type is an unsigned type, the expression ~E is equivalent
to the maximum value representable in that type minus E.

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

57

帖子

1

粉丝