Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
bit fiddling avoided by string manipulation solution in Clear category for Multiplication Table by maybe
def checkio(first, second):
"""ok, let's see:
. let lb and lz be the count of bits and zeros for the left number and
. let rnum and rcom be the value and complement of the right number
then our desired result is:
(and) lb*rnum + (or) lb*(rnum+rcom)+lz*rnum + (xor) lb*rcom + lz *rnum
= (lb+lb+lz+lz)*rnum + (lb+lb)*rcom
"""
# let python (str) do the bit fiddling
lblz,lb = len(f'{first:b}'), sum(int(c) for c in f'{first:b}')
rcom = int(f'{second:b}'.translate({ord('0'):'1',ord('1'):'0'}),2)
return 2*lblz*second + 2*lb*rcom
Jan. 5, 2020
Comments: