python crc模二除法程序求余数
时间: 2024-01-23 08:49:44 浏览: 41
以下是 Python 中实现 CRC 模二除法求余数的程序示例:
```python
def crc_remainder(dividend, divisor):
# 首先将余数设置为被除数
remainder = dividend
# 循环除法
for i in range(len(dividend) - len(divisor) + 1):
# 如果这一位是 1,则进行异或操作
if remainder[i] == '1':
remainder = remainder[:i] + ''.join(['1' if remainder[j] == divisor[j - i] else '0' for j in range(i, i + len(divisor))]) + remainder[i+len(divisor):]
# 返回余数
return remainder[-len(divisor):]
```
其中,`dividend` 表示被除数,`divisor` 表示除数。这个函数会返回除法的余数。注意,在这个函数中,被除数和除数都需要是二进制字符串形式。
相关问题
python crc校验模二除法程序求余数
下面是Python实现的CRC校验模二除法程序求余数的代码示例:
```python
def crc_remainder(input_bitstring, polynomial_bitstring, initial_filler='0'):
# 将数据转换为比特串
polynomial = int(polynomial_bitstring, 2)
fill = int(initial_filler, 2)
data = int(input_bitstring, 2)
# 计算多项式的比特位数
polynomial_bit_count = polynomial_bitstring.count('1')
# 将数据左移多项式比特位数的长度
data <<= (polynomial_bit_count - 1)
# 计算 CRC 校验码
while data.bit_length() >= polynomial_bit_count:
# 用多项式除数据
quotient, remainder = divmod(data, polynomial)
# 将余数左移一位
remainder <<= 1
# 将余数与下一个比特位相加
remainder |= int(input_bitstring[data.bit_length()-polynomial_bit_count])
# 更新数据
data = remainder
# 返回校验码的比特串
return bin(data ^ fill)[2:].zfill(len(initial_filler))
```
其中,`input_bitstring` 是待校验的数据比特串,`polynomial_bitstring` 是生成多项式的比特串,`initial_filler` 是初始填充的比特串。函数返回的是 CRC 校验码的比特串。
使用示例:
```python
# 待校验的数据比特串
data = '1101011011'
# 生成多项式的比特串
polynomial = '1101'
# 初始填充的比特串
filler = '000'
# 计算 CRC 校验码
crc = crc_remainder(data, polynomial, filler)
print(crc) # 输出:101
```
以上代码实现了一个简单的 CRC 校验算法,可以根据需要进行修改和扩展。
python crc模二除法程序
以下是 Python 中的 CRC 模二除法程序示例:
```python
def crc_div(dividend, divisor):
# 将被除数左移除数的位数
dividend = dividend + "0" * (len(divisor) - 1)
remainder = dividend[:len(divisor)] # 取出余数
for i in range(len(divisor), len(dividend)):
if remainder[0] == "1":
# 将余数与除数异或作为新的余数
remainder = "".join([str(int(remainder[j]) ^ int(divisor[j]))) + remainder[len(divisor):]
else:
# 如果余数的最高位为0,则用下一位补上。
remainder = remainder[1:] + dividend[i]
i += 1
# 检查最后的余数是否为0
if remainder[0] == "1":
return False
else:
return True
```
上述函数需要传入两个参数:被除数和除数。其中,被除数和除数均为二进制字符串。函数返回 True 或 False,表示被除数是否可以被除数整除。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)