]> sigrok.org Git - libsigrokdecode.git/blobdiff - decoders/common/srdhelper/mod.py
graycode: Move bitpack/bitunpack to common/.
[libsigrokdecode.git] / decoders / common / srdhelper / mod.py
index 4871205f08261aad2786a175ee2fe48687f5941a..b559c95edc5f280ae727fb6c34a372143b01be17 100644 (file)
 # Return the specified BCD number (max. 8 bits) as integer.
 def bcd2int(b):
     return (b & 0x0f) + ((b >> 4) * 10)
+
+def bitpack(bits):
+    res = 0
+    for i, b in enumerate(bits):
+        res |= b << i
+    return res
+
+def bitunpack(num, minbits=0):
+    res = []
+    while num or minbits > 0:
+        res.append(num & 1)
+        num >>= 1
+        minbits -= 1
+    return tuple(res)