python——原码转补码

#原码转化为补码
def true2complement(str_bin):
    str_new = ""
    flag = True
    if str_bin[0] == "0":
        complement = str_bin
        
    else:
        for i in str_bin[1:]:
            if i == "0":
                str_new += "1"
            else:
                str_new += "0"
        str_flash = '1' + str_new    #反码
        i = len(str_flash) -1    
        while(flag):        
            if(str_flash[i] == '1'):           
                i -= 1        
            elif (str_flash[i] == '0'):            
                flag = False    
        
        complement = str_flash[0:i] + '1' + (len(str_flash)-i-1)*'0'
    print(complement)
    return complement

你可能感兴趣的:(verilog)