From c413347e757ede98757d0f5d629a73fb0736ed26 Mon Sep 17 00:00:00 2001 From: Uwe Hermann Date: Sat, 9 Dec 2017 17:25:53 +0100 Subject: graycode: Move bitpack/bitunpack to common/. --- decoders/common/srdhelper/mod.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'decoders/common/srdhelper/mod.py') diff --git a/decoders/common/srdhelper/mod.py b/decoders/common/srdhelper/mod.py index 4871205..b559c95 100644 --- a/decoders/common/srdhelper/mod.py +++ b/decoders/common/srdhelper/mod.py @@ -20,3 +20,17 @@ # 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) -- cgit v1.2.3-70-g09d2