diff options
author | pacien | 2018-11-28 15:20:14 +0100 |
---|---|---|
committer | pacien | 2018-11-28 15:20:14 +0100 |
commit | d661132528d5c27148a0b55d52709ce97124000a (patch) | |
tree | 31aeb46872fd70b409633e163c0bc0bfbb825429 /src/integers.nim | |
parent | 3d44208aaaeca516eb08a90c98635543cae2bd4d (diff) | |
download | gziplike-d661132528d5c27148a0b55d52709ce97124000a.tar.gz |
add huffman tree structure and serialisation
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 |