编写函数实现:clear all but least signficant n bits of x

方法1:

int lowe_bits(int x, int n)
{
    int w = sizeof(int) << 3;
    unsigned y = ~0;
    y >>= (w - n);
    return y & x;    
}

方法2:

int lower_bits(int x, int n)
{
    int mask = ~(0 - (1 << n - 1) -( 1 << n - 1));
    return mask & x;
}


你可能感兴趣的:(编写函数实现:clear all but least signficant n bits of x)