move pack/unpack_poly to separate file
authorJacob Lifshay <programmerjake@gmail.com>
Tue, 15 Mar 2022 05:24:12 +0000 (22:24 -0700)
committerJacob Lifshay <programmerjake@gmail.com>
Tue, 15 Mar 2022 05:24:12 +0000 (22:24 -0700)
pack_poly.py [new file with mode: 0644]

diff --git a/pack_poly.py b/pack_poly.py
new file mode 100644 (file)
index 0000000..f5ab644
--- /dev/null
@@ -0,0 +1,19 @@
+"""Polynomials with GF(2) coefficients."""
+
+
+def pack_poly(poly):
+    """`poly` is a list where `poly[i]` is the coefficient for `x ** i`"""
+    retval = 0
+    for i, v in enumerate(poly):
+        retval |= v << i
+    return retval
+
+
+def unpack_poly(v):
+    """returns a list `poly`, where `poly[i]` is the coefficient for `x ** i`.
+    """
+    poly = []
+    while v != 0:
+        poly.append(v & 1)
+        v >>= 1
+    return poly