]> sigrok.org Git - libsigrokdecode.git/blobdiff - decoders/common/srdhelper/mod.py
common: rephrase bitpack(), use list comprehension Python idiom
[libsigrokdecode.git] / decoders / common / srdhelper / mod.py
index e65ab17e5ae590ae6854f6edddd7b4acd1dfc143..e37345a0b34f1a416c5c6b407cc746ad9b80ae4c 100644 (file)
 ## GNU General Public License for more details.
 ##
 ## You should have received a copy of the GNU General Public License
-## along with this program; if not, write to the Free Software
-## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+## along with this program; if not, see <http://www.gnu.org/licenses/>.
 ##
 
 # Return the specified BCD number (max. 8 bits) as integer.
 def bcd2int(b):
     return (b & 0x0f) + ((b >> 4) * 10)
+
+def bin2int(s: str):
+    return int('0b' + s, 2)
+
+def bitpack(bits):
+    return sum([b << i for i, b in enumerate(bits)])
+
+def bitunpack(num, minbits=0):
+    res = []
+    while num or minbits > 0:
+        res.append(num & 1)
+        num >>= 1
+        minbits -= 1
+    return tuple(res)