python中的2的补码功能

时间:2019-10-29 11:00:27

标签: python list

我想定义一个函数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

0 个答案:

没有答案