位运算(&)一题

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));
posted @ 2015-01-14 12:20  阿青1987  阅读(141)  评论(0编辑  收藏  举报