source: utilitiespy/tudelft/utilities/tools/calc.py@ 1214

Last change on this file since 1214 was 1207, checked in by wouter, 2 months ago

replaced asbyte with truncate which is more general

File size: 258 bytes
RevLine 
[1206]1
2
[1207]3def truncate(n:int, nbits: int):
[1206]4 '''
[1207]5 keep only the lowest nbits of n. This effectively keeps the number signed
6 but weird things will happen if n does not fit in nbits
[1206]7 '''
[1207]8 half = 1<<(nbits-1)
9 return ((n+half)& ((1<<nbits)-1)) - half
Note: See TracBrowser for help on using the repository browser.