python 获取 一个正整数的二进制

#coding=utf-8

def getbin(a):
    out = ""
    # 辗转相除法
    while (1):
        div = a // 2
        mod = a % 2
        out += str(mod)
        if (div == 0):
            break
        a = div
    return out[::-1]

print(getbin(11))

输出

1011

参考:

https://www.nuoweb.com/scripts/3158.html

https://jingyan.baidu.com/article/f0e83a255ca20422e59101f5.html

猜你喜欢

转载自www.cnblogs.com/sea-stream/p/11324105.html
今日推荐