From 8673a1aab78e5be0caf28faec0e42fd9186d795b Mon Sep 17 00:00:00 2001 From: Jacob Lifshay Date: Mon, 14 Mar 2022 22:24:12 -0700 Subject: [PATCH] move pack/unpack_poly to separate file --- pack_poly.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 pack_poly.py diff --git a/pack_poly.py b/pack_poly.py new file mode 100644 index 0000000..f5ab644 --- /dev/null +++ b/pack_poly.py @@ -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 -- 2.30.2