本文介绍了需要帮助,在蟒蛇添加二进制数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有以二进制形式2号作为一个字符串,我想补充他们,我一定会做到位由数字,从最右边的结束。所以001 + 010 = 011
但是假设我要做001 + 001,我应该怎么创建一个code弄清楚如何采取结转的反应?
If I have 2 numbers in binary form as a string, and I want to add them I will do it digit by digit, from the right most end. So 001 + 010 = 011But suppose I have to do 001+001, how should I create a code to figure out how to take carry over responses?
推荐答案
斌
和 INT
是非常有用的在这里
bin
and int
are very useful here:
a = '001'
b = '011'
c = bin(int(a,2) + int(b,2))
# 0b100
INT
允许你指定的第一个参数是一个字符串转换时(在这种情况下,二)在什么基础,而斌
将数字转换回一个二进制字符串。
int
allows you to specify what base the first argument is in when converting from a string (in this case two), and bin
converts a number back to a binary string.
这篇关于需要帮助,在蟒蛇添加二进制数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!