Skip to content

Instantly share code, notes, and snippets.

@faissaloo
Created March 22, 2020 00:57
Show Gist options
  • Save faissaloo/c1354d3aa0d07ff35ece67d6543696ae to your computer and use it in GitHub Desktop.
Save faissaloo/c1354d3aa0d07ff35ece67d6543696ae to your computer and use it in GitHub Desktop.
16-bit multiplication of 8 bit integers via partial products
#!/usr/bin/env python3
def partial_product(a_bytes, b_bytes):
high_a = a_bytes[0]
low_a = a_bytes[1]
high_b = b_bytes[0]
low_b = b_bytes[1]
partials = [
low_a*low_b,
low_a*high_b<<4,
high_a*low_b<<4,
high_a*high_b<<8
]
return sum(partials)
result = partial_product([0x3,0xF], [0xE, 0x3])
print("Result:",result)
print("Expected:",0x3F*0xE3)
print(hex(0x3F*0xE3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment