Skip to content

Instantly share code, notes, and snippets.

@AlexCMueller
Created July 4, 2018 03:39
Show Gist options
  • Save AlexCMueller/6ad27454b4199f9a2ef404f15509897b to your computer and use it in GitHub Desktop.
Save AlexCMueller/6ad27454b4199f9a2ef404f15509897b to your computer and use it in GitHub Desktop.
def polymult(a, b):
exp = 10
ares = 0 # evaluate a at x
for ac in a:
ares = (ares<<exp) + ac
bres = 0 # evaluate b at x
for bc in b:
bres = (bres<<exp) + bc
prod = ares * bres
result = []
while prod != 0:
if prod&(1<<(exp-1)): # if flag bit is set
result.append(-((~prod+1)&(1<<exp)-1))
prod += 1<<exp # tfw 2s compliment
else:
result.append(prod & (1<<exp)-1)
prod = prod>>exp
return result
print(polymult([6,-6,-5],[7,6,-5]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment