python: Binary Number

 

   binary_num = '10000001'
    ones_complement = ''
    for bit in binary_num:
        ones_complement += '0' if bit == '1' else '1'
    print(ones_complement)

    #加上1
    binary_number='10000011'
    decimal = 0
    newstr=''
    for i in range(len(binary_number)):
        if binary_number[i] == '1':
            newstr +='0'
            decimal += 2 ** i
        else:
            newstr +='1'
            decimal += 0 ** i
    print(newstr)

    print("{0:b}".format(decimal))
    decimal = decimal + 1
    print("{0:b}".format(decimal))
    c='10000001'
    print(int(c,2)) # 二进制转十进制
    a='11001000'
    b = int(a, 2) # 二进制转十进制
    b+=10
    print(b)
    print(bin(b)) #十进制转二进制
    mm=0b1100100
    mm<< 1 #左移位  >> 右移位
    print('move 1:',bin(mm))
    print('move 1:', "{0:b}".format(mm))
    print("{0:b}".format(b)) #十进格式化二进制

    decimal = 200
    print("{0:b}".format(decimal))
    decimal = decimal + 1
    print("{0:b}".format(decimal))
    fl=[]
    kk="{0:b}".format(decimal)
    for ss in kk:
        fl.append(int(ss))
    print(fl)

  

 

 

 

def binary_to_decimal(binary_number):
    """

    :param binary_number:
    :return:
    """
    decimal = 0
    for i in range(len(binary_number)):
        if binary_number[i] == '1':
            decimal += 2 ** i
    return decimal
def one_complement(binary_number):
    """

    :param binary_number:
    :return:
    """
    decimal = binary_to_decimal(binary_number)
    return decimal + 1

def pagingNum(ptotal, verypnum):
    """
    打包装的箱数计算
    :param ptotal: 总数量
    :param verypnum: 每个包装打包的数量
    :return: 返回包装数量
    """
    pagenumber=0
    for k in range(0,ptotal,verypnum):
        pagenumber+=1
    return  pagenumber


# Returns '0' for '1' and '1' for '0'
def flip(c):
    return '1' if (c == '0') else '0'


# Print 1's and 2's complement of
# binary number represented by "bin"
def printOneAndTwosComplement(bin):
    n = len(bin)
    ones = ""
    twos = ""

    # for ones complement flip every bit
    for i in range(n):
        ones += flip(bin[i])

        # for two's complement go from right
    # to left in ones complement and if
    # we get 1 make, we make them 0 and
    # keep going left when we get first
    # 0, make that 1 and go out of loop
    ones = list(ones.strip(""))
    twos = list(ones)
    for i in range(n - 1, -1, -1):

        if (ones[i] == '1'):
            twos[i] = '0'
        else:
            twos[i] = '1'
            break

    i -= 1
    # If No break : all are 1 as in 111 or 11111
    # in such case, add extra 1 at beginning
    if (i == -1):
        twos.insert(0, '1')

    print("1's complement: ", *ones, sep="")
    print("2's complement: ", *twos, sep="")

  

调用:

 binary_num = '101010'
    ones_complement = ''
    for bit in binary_num:
        ones_complement += '0' if bit == '1' else '1'
    print(ones_complement)

    binary_number = '101010'
    one_complement(binary_number)
    print(ones_complement)

    bin = "101010"
    printOneAndTwosComplement(bin.strip(""))



    pag=56
    co=pagingNum(pag,12)
    print(co)

  

    binary_num = '101010'
    ones_complement = ''
    for bit in binary_num:
        ones_complement += '0' if bit == '1' else '1'
    print(ones_complement)

    binary_number = '101010'
    one_complement(binary_number)
    print(ones_complement)

    bin = "101010"
    printOneAndTwosComplement(bin.strip(""))

    decimal = 0
    for i in range(len(binary_number)):
        if binary_number[i] == '1':
            decimal += 2 ** i
    print("0{0:b}".format((decimal)))
    decimal=decimal+1
    print("0{0:b}".format((decimal)))

    pag=56
    co=pagingNum(pag,12)
    print(co)

  

 

def asce(dulist:list)->bool:
    """
    是否升序
    :param dulist:
    :return:
    """
    isasc=False
    ch = dulist[0]
    asc = 1  # 升序记数器 ascending or descending
    l = len(dulist)
    for k in range(1, len(dulist)):
        if (ch < dulist[k]):
            ch = dulist[k]
            asc += 1
    if(asc==l):
        isasc=True
    return isasc


def desc(dulist: list) -> bool:
    """
    是否降序
    :param dulist:
    :return:
    """
    isdes = False
    ch = dulist[0]
    des = 1  # 降序记数器 descending
    l = len(dulist)
    for k in range(1,len(dulist)):
        if(ch>dulist[k]):
            ch = dulist[k]
            des+=1
    if (des == l):
        isdes = True

    return isdes

  

    ls=[5,7,20,50,70]
    ch=ls[0]
    asc=1 #升序记数器 ascending or descending
    des=1 #降序记数器
    l=len(ls)
    for k in range(1,len(ls)):
        if(ch<ls[k]):
            ch=ls[k]
            asc+=1

    ch = ls[0]
    for k in range(1,len(ls)):
        if(ch>ls[k]):
            ch = ls[k]
            des+=1
    print(asc,des)
    if(asc==l):
        print("asc")
    if(des==l):
        print("des")
    print(asce(ls))
    print(desc(ls))

  

def clen(custome)->int:
    """
    计算变量的长度函数
    :param custome:
    :return:
    """
    num=0
    #print(type(custome))
    if isinstance(custome,list): #列表 集合
        for k in custome:
            #print(k)
            num+=1
    if isinstance(custome,str): # 字符串 enumerate() iter()遍历字符串
        for k in custome:
            #print(k)
            num+=1
    if isinstance(custome,int): #如果是整数,计算它的长度
        if(custome==0):
            num=0
        else:
            num=1+clen(custome//10)
    return num

  

    ls = [5, 7, 20, 50, 70]
    print(clen(ls))
    ddd="hihowareyou"
    print(clen(ddd))
    print(clen(1023))

  

 

posted @ 2024-05-12 22:20  ®Geovin Du Dream Park™  阅读(5)  评论(0编辑  收藏  举报