首页 > 代码库 > 位运算(&)一题

位运算(&)一题

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));

位运算(&)一题