| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 495 人关注过本帖
标题:请高手帮我解惑!
只看楼主 加入收藏
format_yang
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-8-24
结帖率:0
收藏
已结贴  问题点数:10 回复次数:1 
请高手帮我解惑!
#include <stdio.h>
int main(void)
{
    unsigned int original = 0xABC;
    unsigned int result = 0;
    unsigned int mask = 0xF; /* Righmost four bits */

    printf("\n original = %X", original);

    /* Insert first digit in result */
    result |= original&mask;    /* Put right 4 bits from original in result */

    /* Get second digit */
    original >>= 4 ;            /* Shift original right four positions */
    result <<= 4 ;                /* Make from for next digit */
    result |= original&mask;    /* Put right 4 bits from original in result */

    /* Get third digit */
    original >>= 4;                /* Shift original right four positions */
    result <<= 4;                /* Make from for next digit */
    result |= original&mask;    /* Put right 4 bits from original in result */
    printf("\t result = %X\n", result);
    return 0;
}

请高手帮我解惑,这个result |= original&mask得到的值的二进制是0000 0000 0000 1100,
original = 0xABC 换成二进制也是0000 1010 1011 1100,这个值经过和mask的值0000 0000 0000 1111按位与运算得到上面的结果。但是我按着得到的结果算下去,最后输出的结果并不是CBA,请高手解释下,感谢!

[ 本帖最后由 format_yang 于 2012-8-25 16:25 编辑 ]
搜索更多相关主题的帖子: first mask original include result 
2012-08-24 22:57
stophin
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:3
帖 子:227
专家分:618
注 册:2010-3-26
收藏
得分:10 
original&mask的值都是取original的最后4位,其余位为0,result先向前移4位,然后做|=操作,就相当于将前面与的结果的后四位,
放到result移出来的后4位上,推算结果如下:
#include <stdio.h>
int main(void)
{
    unsigned int original = 0xABC;                            //0000 1010 1011 1100
    unsigned int result = 0;                                  //0000 0000 0000 0000
    unsigned int mask = 0xF; /* Righmost four bits */         //0000 0000 0000 1111

    printf("\n original = %X", original);

    /* Insert first digit in result */
    result |= original&mask;    /* Put right 4 bits from original in result */ //result=0000 0000 0000 1100

    /* Get second digit */
    original >>= 4 ;            /* Shift original right four positions */      //original=0000 0000 1010 1011
    result <<= 4 ;                /* Make from for next digit */               //result  =0000 0000 1100 0000
                                                                               //与结果  =0000 0000 0000 1011
    result |= original&mask;    /* Put right 4 bits from original in result */ //后四位  =0000 0000 1100 1011

    /* Get third digit */
    original >>= 4;                /* Shift original right four positions */   //original=0000 0000 0000 1010
    result <<= 4;                /* Make from for next digit */                //result  =0000 1100 1011 0000
                                                                               //与结果  =0000 0000 0000 1010
    result |= original&mask;    /* Put right 4 bits from original in result */ //后四位  =0000 1100 1011 1010
                                                                               //结果刚好将ABC逆序了下变成CBA
    printf("\t result = %X\n", result);
    return 0;
}
2012-08-25 17:25
快速回复:请高手帮我解惑!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.034843 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved