gpt4 book ai didi

CRC 实现特定的多项式。多项式与代码中使用的多项式有何关系?

转载 作者:太空宇宙 更新时间:2023-11-04 06:11:46 26 4
gpt4 key购买 nike

我有以下 CRC 函数:

#define CRC8INIT    0x00
#define CRC8POLY 0x18 //0X18 = X^8+X^5+X^4+X^0

// ----------------------------------------------------------------------------

uint8_t crc8 (uint8_t *data, uint16_t number_of_bytes_in_data)
{
uint8_t crc;
uint16_t loop_count;
uint8_t bit_counter;
uint8_t b;
uint8_t feedback_bit;

crc = CRC8INIT;

for (loop_count = 0; loop_count != number_of_bytes_in_data; loop_count++) {
b = data[loop_count];
bit_counter = 8;

do {
feedback_bit = (crc ^ b) & 0x01;

if (feedback_bit == 0x01) {
crc = crc ^ CRC8POLY;
}

crc = (crc >> 1) & 0x7F;

if (feedback_bit == 0x01) {
crc = crc | 0x80;
}

b = b >> 1;
bit_counter--;

} while (bit_counter > 0);
}

return crc;
}

0x18 与多项式 X^8+X^5+X^4+X^0 有什么关系?

X^8+X^5+X^4+X^0 = 100110001

0x18 = 00011000

如果我将 CRC8POLY 定义为 0xEA(我已经看到了),那会代表什么多项式呢?

最佳答案

How does 0x18 relate to the polynomial X^8+X^5+X^4+X^0?

由于代码是右移CRC,所以每个字节的“最高位”是bit 0而不是bit 7。poly需要从100110001反转为100011001,即0x119,右移后bit 0x119 的 0 被移出,因此可以改用 0x118。如果反馈位为 1,则代码使用第二个 if 语句 to or in (0x100) >> 1 == 0x80。作为替代方案,由于 feedback_bit 为 0 或 1,则 (0-feeback_bit) 可用作掩码 (假设对多边形的二进制补码数学),而不是使用 if 语句。

typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
#define CRC8INIT 0x00
#define CRC8POLY 0x8c // 0x119 >> 1

uint8_t crc8 (uint8_t *data, uint16_t number_of_bytes_in_data)
{
uint8_t crc;
uint16_t loop_count;
uint8_t bit_counter;
uint8_t b;
uint8_t feedback_bit;

crc = CRC8INIT;

for (loop_count = 0; loop_count != number_of_bytes_in_data; loop_count++) {
b = data[loop_count];
bit_counter = 8;
do {
feedback_bit = (crc ^ b) & 0x01;
crc = (crc >> 1) ^ ((0-feedback_bit) & CRC8POLY);
b = b >> 1;
bit_counter--;
} while (bit_counter > 0);
}

return crc;
}

关于CRC 实现特定的多项式。多项式与代码中使用的多项式有何关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54535088/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com