首页 > 解决方案 > python中2的补码函数

问题描述

我想定义一个函数umklappen,它将以 10 为基数的双重表示形式作为列表并翻转位 1->0 和 0->1。我遇到了类型ntobasetwo(n,c)是类的问题NoneType。我不确定为什么会这样,以及在我的情况下如何解决这个问题。

# Funktion, die ganze Zahlen im Dezimalsystem als Zahlen im Dualsystem darstellt
import numpy as np
import math

# Decimal number is converted into binary by dividing the number successively by 2
# and printing the remainder in reverse order
def  ntobasetwo(n,c):
    binary = []
    while n!= 0:
        bit = n%2
        binary.insert(0, bit)
        n = n//2
    if len(binary)>c:
        binary = binary[0:c]
    print(binary)

bin_1248 = ntobasetwo(1248,5)

def umklappen(binList):
    for i in range(len(binList)):
        if binList[i] == 0:
            binList[i] = 1
        else:
            binList[i] = 0
    print(binList)

umklappen_bin_1248 = umklappen(bin_1248)
umklappen_bin_1248

标签: pythonlist

解决方案


推荐阅读