位运算与嵌入式编程

1、找出下面程序的错误,并解释它为什么是错误的。

//The function need set corresponding bit int 0
#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));
正确的程序如下所示:

#include
#include

//The function need set corresponding bit int 0
#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;
}
int main()
{
    unsigned int x = 0xffffffff;
    unsigned char y = 4;
    Bit_Reset(&x, y);
    std::cout<<std::hex<'\n';
    system("pause");
    return 0;
}

2、以下代码哪个等同与int i = (int)p;(p的定义为char *p)
A、int i = dynamic_cast(p)
B、int i = static_cast(p)
C、int i = const_cast(p)
D、int i = reinterpret_cast(p)

你可能感兴趣的:(位运算与嵌入式编程)