source:
utilitiespy/tudelft/utilities/tools/calc.py
Last change on this file was 1207, checked in by , 3 weeks ago | |
---|---|
File size: 258 bytes |
Rev | Line | |
---|---|---|
[1206] | 1 | |
2 | ||
[1207] | 3 | def 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.