Someone
public text v1 · immutabledef extend(old, newlow, w=8):
mask = (1<<w)-1
maxdelta = mask/2
oldhigh = old & ~mask
oldlow = old & mask
if oldlow < newlow:
candidate1 = oldhigh + newlow
candidate2 = oldhigh + newlow - (1<<w)
else:
candidate1 = oldhigh + newlow
candidate2 = oldhigh + newlow + (1<<w)
if abs(old - candidate1) > maxdelta:
return candidate2
else:
return candidate1