def truncate(n:int, nbits: int): ''' keep only the lowest nbits of n. This effectively keeps the number signed but weird things will happen if n does not fit in nbits ''' half = 1<<(nbits-1) return ((n+half)& ((1<