def shift_into_range(val, ctype):
    """Shift a numerical value into range for given integral ctype."""
    if ctype.signed:
        max_val = 1 << ctype.size * 8 - 1
        range = 2 * max_val
    else:
        max_val = 1 << ctype.size * 8
        range = max_val
    val = val % range
    if val >= max_val:
        val -= range
    return val
