diff options
Diffstat (limited to 'src/integers.nim')
-rw-r--r-- | src/integers.nim | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/src/integers.nim b/src/integers.nim index 7b0f166..c93c9b8 100644 --- a/src/integers.nim +++ b/src/integers.nim | |||
@@ -22,6 +22,12 @@ proc `/^`*[T: Natural](x, y: T): T = | |||
22 | proc truncateToUint8*(x: SomeUnsignedInt): uint8 = | 22 | proc truncateToUint8*(x: SomeUnsignedInt): uint8 = |
23 | (x and uint8.high).uint8 | 23 | (x and uint8.high).uint8 |
24 | 24 | ||
25 | proc bitLength*[T: SomeUnsignedInt](x: T): int = | ||
26 | var buf = x | ||
27 | while buf > 0.T: | ||
28 | buf = buf shr 1 | ||
29 | result += 1 | ||
30 | |||
25 | proc leastSignificantBits*[T: SomeUnsignedInt](x: T, bits: int): T = | 31 | proc leastSignificantBits*[T: SomeUnsignedInt](x: T, bits: int): T = |
26 | let maskOffset = sizeof(T) * wordBitLength - bits | 32 | let maskOffset = sizeof(T) * wordBitLength - bits |
27 | if maskOffset >= 0: (x shl maskOffset) shr maskOffset else: x | 33 | if maskOffset >= 0: (x shl maskOffset) shr maskOffset else: x |