Created
July 4, 2018 03:39
-
-
Save AlexCMueller/6ad27454b4199f9a2ef404f15509897b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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