def encode_syncsafe32(i):
    """given a 28 bit int, returns a 32 bit value
    with sync-safe bits added

    may raise ValueError is the value is negative
    or larger than 28 bits"""
    if i >= 2 ** 28:
        raise ValueError('value too large')
    elif i < 0:
        raise ValueError('value cannot be negative')
    value = 0
    for x in range(4):
        value |= (i & 127) << x * 8
        i >>= 7
    return value
