#原码转化为补码
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
版权声明:本文为cherry1307原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。