码迷,mamicode.com
首页 > 其他好文 > 详细

位运算(&)一题

时间:2015-01-14 14:03:02      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

Find the defects in each of the following programs, and explain why it is incorrect.

// the function need set corresponding bit in int
#define BIT_MASK(bit_pos)      (0x01 << (bit_pos))

int Bit_Reset(unsigned int *val, unsigned char pos)
{
    if (pos >= sizeof(unsigned int)*8)
    {
        return 0;
    }

    *val = (*val && ~BIT_MASK(pos));

    return 1;
}

析:

*val = (*val && ~BIT_MASK(pos));

是逻辑运算而不是位运算
按如下修改即可

*val = (*val & ~BIT_MASK(pos));

位运算(&)一题

标签:

原文地址:http://www.cnblogs.com/aqing1987/p/4223623.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!