简单的十进制转二进制的实现

版权声明:转载请附上链接 https://blog.csdn.net/python_neophyte/article/details/82286705

目前只实现了正数10进制 –> 2进制

def binary(x):
    def get(g,h):
        return g*10+h
    L=[]
    L.append(x)
    if isinstance(x,int):
        while x>1:
            x=x//2
            a=x
            L.append(a)
        LL=L
        S=[]
        for i in LL:
            remainder=i%2
            S.append(remainder)
        SS=S[::-1]
        return reduce(get,SS)
    else:
        print('请输入非字符串的整数')

负数还涉及到了反码,补码的知识。比较复杂,先这样吧,有更方便的方法,请在评论区斧正。

猜你喜欢

转载自blog.csdn.net/python_neophyte/article/details/82286705