Question

python.. write a program which does multiplication on binary numbers

python.. write a program which does multiplication on binary numbers

0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

Note: Brother sometimes while uploading on HomeworkLib the indentations change. So, I request you to verify once with the screenshot.

# encode int to binary list
def bn(n):
return list(reversed(list(map(lambda x: 1 if x == "1" else 0, "{0:b}".format(n)))))

def is_zero(n):
return not n[1:] and not n[0]

def is_one(n):
return not n[1:] and n[0]

# multiplication
def multiply2(a, b):
def _(c, d, f, g, e):
if not b and not c:
return g + (e if e[1] else e[:1])
if c:
# clumsy part is the formation of A and x
A = 0 if (0 if (1 if c[0] and d else 0) == (e[1] if e else 0) else 1) == (f[0] if f else 0) else 1
# x = [0, 0] or [1, 0] or [0, 1] or [1, 1]
x = [A, 1 if ((e[1] if e else 0) or (f[0] if f else 0)) and c[0] and d or \
((e[1] if e else 0) and (f[0] if f else 0)) and not c[0] and d else 0]
#print(x, c, d, e, f, g)
return _(c[1:], d, f[1:], (g + e[:1] if e else g), x)
return g[:1] + _(a, b.pop(0), g[1:] + e, [], [])
return [0] if is_zero(a) or is_zero(b) else \
b if is_one(a) else \
a if is_one(b) else _(a, b.pop(0), [], [], [])

print(multiply2(bn(101), bn(11)))

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
python.. write a program which does multiplication on binary numbers
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT