first off, I hope it's not against the rules for me to request optimization... :x

I've tried to write the code with my best understandings of python optimization (which isn't much).

def ov( value, byte_size ):
    sign = False
    if value<0: value += 1; sign = True
    value = abs( value ) & ((1<<((byte_size<<3)-1))-1)
    if sign: value = (value*-1)-1
    return value

the code simply deals with memory overflow of signed ints by forcing their absolute value to fit in the required size limits.

for example, 258 (0b100000010) would fit into an 8-bit signed int as 2

optimized it to:

def ov(value,byte_size):
    sign = value<0
    return (((-1)**sign)*(abs(value+sign)%(1<<((byte_size<<3)-1))))-sign

though more can prbly be done