- 论坛徽章:
- 0
|
想实现将两个字符型的数保存在一个short类型变量中,遇到了问题
- #include <stdio.h>
- void main(void)
- {
- #if 0
- unsigned char cKeyscan = 0x7f;
- unsigned char cKeyread = 0xf7;
- unsigned short sPos;
- #else
- char cKeyscan = 0x7f;
- char cKeyread = 0xf7;
- short sPos;
- #endif
- sPos = cKeyscan;
- sPos = sPos << 8;
- sPos &= 0xFF00;
- sPos |= cKeyread;
- printf("sPos:%0x\r\n", sPos);
- }
复制代码
位运算的时候若变量为有符号类型时
sPos |= cKeyread;
编译器做了如下的转换
sPos |= (long)cKeyread;//高位为1,负数,cKeyread变为了fffffff7(值就成了FFFFFFF7不理解)
得出教训了,以后位运算还是用unsigned好点!不需要考虑什么符号问题了
[ 本帖最后由 天冷就回来 于 2007-5-9 20:58 编辑 ] |
|