python - 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
解决方案
推荐阅读
- activiti - Activiti V6.0 数据库查询导致生产环境中的一些最大 CPU 峰值
- angular - Angular - 内容提供者
- java - Azure Function App:@EventHubTrigger 为每个 EventHubTrigger 函数设置不同的批处理大小
- c# - 为什么 OracleConnection.Open() 的实例在将 Oracle DB 从 11g 升级到 19c 后会抛出错误?
- python - 打印日期范围之间的所有星期四
- redirect - 将输出重定向到某个文件,同时在 Python 中重定向文件的 tail -f
- python - 使用 pytest 进行单元测试架构
- python - 如何按分组总和划分熊猫列中的元素
- python - 创建一个python函数来改变列的顺序
- python - 试图在 ubuntu 上为 django 设置 mysql 但出现错误 'django.db.utils.OperationalError: (1046, 'No database selected')'