Return 1 when x can be represented as an n-bit, 2's complment number; 0 otherwise

int fits_bits(int x, int n)
{
    int count = sizeof(int) << 3 - n;
    return ((x << count) >> count) == x;
}

你可能感兴趣的:(Return 1 when x can be represented as an n-bit, 2's complment number; 0 otherwise)