CRC实现特定的多项式.多项式与代码中使用的多项式有什么关系? [英] CRC implementing a specific polynomial. How does the polynomial relate to the polynomial used in code?

查看:105
本文介绍了CRC实现特定的多项式.多项式与代码中使用的多项式有什么关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下CRC功能:

I have the following CRC function:

#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有何关系?

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

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

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

0x18 = 00011000

0x18 = 00011000

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

What if I define CRC8POLY as 0xEA instead (I have seen this), what polynomial would that represent?

推荐答案

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

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

由于代码是右移CRC,所以每个字节的最高有效位"是位0而不是位7.在右移位之后,poly需要从100110001反转为100011001,即0x119.0x119中的0被移出,因此可以改用0x118.如果反馈位为1,则代码使用第二个if语句进行或在(0x100)>> 1 == 0x80中进行选择.或者,由于feedback_bit为0或1,因此(0-feeback_bit)可以用作掩码(假设多边形是2的补码,而不是使用if语句.

Since the code is a right shifting CRC, the "most significant bit" of each byte is bit 0 instead of bit 7. The poly needs to be reversed from 100110001 to 100011001, which is 0x119, after the right shift, bit 0 of 0x119 is shifted off, so 0x118 can be used instead. The code uses a second if statement to or in (0x100) >> 1 == 0x80 if the feedback bit is 1. As an alternative, since feedback_bit is 0 or 1, then (0-feeback_bit) can be used as a mask (assuming two's complement math) for the poly instead of using an if statement.

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实现特定的多项式.多项式与代码中使用的多项式有什么关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆