首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CRC代码和实现兼容性

CRC代码和实现兼容性
EN

Stack Overflow用户
提问于 2017-01-31 23:52:48
回答 1查看 164关注 0票数 0

CRC校验和的机制或步骤很简单,但是软件在某种程度上很complicated,并且软件中有一些步骤与CRC的步骤不兼容。下图是获得CRC校验和的步骤(这只是模2除法):

校验和是余数= 001

用于计算位串的CRC校验和的软件为:

代码语言:javascript
复制
/*
 * The width of the CRC calculation and result.
 * Modify the typedef for a 16 or 32-bit CRC standard.
 */
typedef uint8_t crc;

#define WIDTH  (8 * sizeof(crc))
#define TOPBIT (1 << (WIDTH - 1))

crc
crcSlow(uint8_t const message[], int nBytes)
{
    crc  remainder = 0; 


    /*
     * Perform modulo-2 division, a byte at a time.
     */
    for (int byte = 0; byte < nBytes; ++byte)
    {
        /*
         * Bring the next byte into the remainder.
         */
        remainder ^= (message[byte] << (WIDTH - 8));

        /*
         * Perform modulo-2 division, a bit at a time.
         */
        for (uint8_t bit = 8; bit > 0; --bit)
        {
            /*
             * Try to divide the current data bit.
             */
            if (remainder & TOPBIT)
            {
                remainder = (remainder << 1) ^ POLYNOMIAL;
            }
            else
            {
                remainder = (remainder << 1);
            }
        }
    }

    /*
     * The final remainder is the CRC result.
     */
    return (remainder);

}

我看到part( remainder<<1 )中的软件存在不兼容性,因为即使后面的位不是0,移位也总是会在右侧添加0。

在这一部分中:remainder ^= (message[byte] << (WIDTH - 8));

当放入第一个字节时,我看不出有什么问题,因为初始值是因为初始值是0,但是当插入下一个字节时,为什么我们要将它们的每个字节与前面的余数进行异或

EN

回答 1

Stack Overflow用户

发布于 2017-02-01 01:26:28

代码示例似乎使用了可变大小的CRC,其中CRC的大小是宽度。多项式是WIDTH+1位多项式的底部宽度位,其最低有效位将设置为1。由于操作是异或运算,因此数据位被异或为余数的顺序并不重要,因此8个数据位可以同时异或为余数的高位。则该比特在时间反馈周期中出现8比特。因为多项式的底部位是1,所以只要数据中有任何1位,就会保持循环。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41961780

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档