def _extract_n_bits(value: int, num_bits: int) -> int:
    """Extracts n bits from a number"""
    limit_bit = 1 << num_bits
    result = value & (limit_bit - 1)
    if value >= limit_bit:
        result |= limit_bit
    return result