Python二进制乘法
Oct 13, 2009今天无聊,在百度知道上看到这个问题:
Python里怎么样用二进制来做乘法 2个数字,先转换成二进制的2个list,然后用这两个list来做乘法,最后转换回数字 于是无比无聊的解答了一下,下面是代码:
N = 32 #the number of bits for an integer
def int2b(n, bit=N):
return [(n >> i) & 1 for i in range(bit)[::-1]]
def b_add(l1, l2, bit=N):
result = [0]*N
carry = 0
for i in range(N)[::-1]:
half_sum = l1[i] ^ l2[i]
b_sum = (half_sum ^ carry)
half_carry = (l1[i] & l2[i])
carry = (carry & half_sum) | half_carry
result[i] = b_sum
return result
def b_multiply(l1, l2, bit=N):
result = [0]*N
for i in range(N):
if l2[i]:
result = b_add(result[:],l1[N-i-1:]+[0]*(N-i-1))
return result
def b2int(l, bit=N):
result = 0
for i in range(bit):
if l[i]:
result += (l[i]<<(N-i-1))
return result
def main(x, y):
print b2int(b_multiply(int2b(x), int2b(y)))
if __name__ == '__main__':
main(5,7)
此代码只用于无符号整数,且没有考虑溢出问题,如果大家有更好的方法,可以到原帖回答一下,有赏金80块。。原帖已关闭=.=! 大家留言好了