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

 

 

posted @ 2019-08-08 21:52  anobscureretreat  阅读(837)  评论(0编辑  收藏  举报