forked from zmbush/nameserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bits.py
36 lines (32 loc) · 821 Bytes
/
bits.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""
Functions to extract and put data into integers. Finer grained than struct
"""
def extractBits(bitlen, number):
"""
Extract Bits from a number. Like struct, but smaller.
"""
totalBits = 0
for bit in bitlen.split():
totalBits += int(bit)
currentBits = 0
extracted = []
for bit in bitlen.split():
currentBits += int(bit)
shifted = number >> totalBits - currentBits
actual = shifted & ~(-1 << int(bit))
extracted.append(actual)
return extracted
def packBits(bitlen, *numbers):
"""
Pack bits into a number
"""
if len(bitlen.split()) != len(numbers):
raise Exception("len(numbers) must equal len(bitlen.split())")
number = 0
number |= numbers[0]
i = 1
for bit in bitlen.split()[1:]:
number <<= int(bit)
number |= numbers[i]
i += 1
return number