1

我被困在一个问题上,尽管我找不到比较是如何出错的。我正在尝试做多项式长除法模2。

while len(binCrcGen) <= len(binPayload) and binPayload:
        if binPayload[0] == binCrcGen[0]:
                del binPayload[0]
                for j in range(len(binCrcGen)-1):
                        if binPayload[j] == binCrcGen[j+1]:
                                binPayload.insert(binPayload[j],0)
                                del binPayload[j+1]
                                print(binPayload, "binPayload")
                        else:
                                binPayload.insert(binPayload[j],1)
                                del binPayload[j+1]
                                print(binPayload, "binPayload")

但是我的输出给了我:

Input the binary data payload: 10010
Input the binary representation of the CRC generator: 11
[1, 0, 1, 0] binPayload
[1, 1, 0] binPayload
[1, 0] binPayload
[1] binPayload -- wrong here 1 xor 1 should bring 0 to list.
[1] Remainder
[1, 1] binCrCGen
The data transmitted with the payload 1001 is protected by the CRC 11 is 10011

当手动计算它时,我得到一个 00 的余数,它变成一个空白列表,因为空白列表被删除了。

4

1 回答 1

2

您的错误在以下几行:

                                binPayload.insert(binPayload[j],0)
                                binPayload.insert(binPayload[j],1)

您没有插入 index j。您在索引 0 或 1 处插入,具体取决于是什么binPayload[j]。要修复它,请将索引更改为j而不是binPayload[j]. 当你在它的时候,你可以删除你正在使用的奇怪的插入/删除东西,然后使用binPayload[j] = whatever. 此外,您可以使用异或运算符^来删除 if/else,并且可以使用就地版本^=来节省一些重复:

binCrcGen  = [1,1]
binPayload = [1,0,0,1,0]

while len(binCrcGen) <= len(binPayload) and binPayload:
    if binPayload[0] == binCrcGen[0]:
        del binPayload[0]
        for j in range(len(binCrcGen)-1):
            binPayload[j] ^= binCrcGen[j+1]
        print(binPayload, "binPayload")
    while binPayload and binPayload[0] == 0:
        del binPayload[0]
        print(binPayload, "binPayload")
于 2013-09-18T06:36:38.173 回答