def correct(value, ty):
    """ Correct a value to the given bits """
    bits = ty.bits
    signed = ty.signed
    base = 1 << bits
    value %= base
    return value - base if signed and value.bit_length() == bits else value