Created
February 6, 2016 03:53
-
-
Save bgulla/46624b8e4468bcce7fab to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/python | |
from prettytable import PrettyTable | |
table = PrettyTable() | |
def btod(n): | |
return int(n,2) | |
hex1="0x62" | |
hex2="0x12" | |
multiplier = bin(int(hex1,0)) | |
multiplicand = bin(int(hex2,0)) | |
answer = int(hex1,0) * int(hex2,0) | |
binaryproduct = bin(0) | |
table.add_row(["Iteration", "Step", "Multiplier", "Multiplicand", "Product"]) | |
table.add_row(["0", "Initial Values", multiplier, multiplicand, binaryproduct]) | |
iteration = 0 | |
while btod(binaryproduct) != answer: | |
iteration = iteration + 1 | |
if bin(int(multiplier,2))[-1] == "1": | |
binaryproduct = bin(int(multiplicand,2) + int(binaryproduct,2)) | |
table.add_row([iteration, "1: 1-> prod = prod + mcand", multiplier, multiplicand, binaryproduct]) | |
else: | |
table.add_row([iteration, "1: 0 -> No Operation", multiplier, multiplicand, binaryproduct]) | |
multiplicand = bin(int(multiplicand,2) << 1) | |
table.add_row([iteration, "2: Shift left multiplicand", multiplier, multiplicand, binaryproduct]) | |
multiplier = bin(int(multiplier,2) >> 1) | |
table.add_row([iteration, "3: Shift right multiplier", multiplier, multiplicand, binaryproduct]) | |
print table |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment