如何计算 CRC 中使用的 XOR 余数?

发布于 2024-07-09 17:03:01 字数 270 浏览 20 评论 0原文

我试图记住如何计算循环冗余检查中的 XOR 算法的剩余部分以验证网络消息的剩余位。

我不应该扔掉那本教科书。

这在代码中很容易完成,但是如何手动计算出来呢?

我知道它看起来像标准除法算法,但我不记得从那里去哪里获得余数。

      ___________
1010 | 101101000

注意:我确实用谷歌搜索了它,但无法找到他们映射计算余数的步骤的地方。

I'm trying to remember how the math is worked out to compute the remainder of an XOR algorithm in Cyclical Redundancy Checks to verify the remainder bits of a network message.

I shouldn't have tossed that text book.

This is easily done in code, but how is it worked out by hand?

I know it looks something like a standard division algorithm, but I can't remember where to go from there to get the remainder.

      ___________
1010 | 101101000

Note: I did google it, but wasn't able to find a place where they mapped the steps in figuring the remainder.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

謸气贵蔟 2024-07-16 17:03:01
1010 | 101101000
       1010
       0001 this result is 1011 XOR 1010 = 0001
          1010
          1010
          0000  thus no remainder. 

因此 101101000 是完美的,传输/接收没有发生错误

1010 | 101101000
       1010
       0001 this result is 1011 XOR 1010 = 0001
          1010
          1010
          0000  thus no remainder. 

Thus 101101000 is perfect and no error has occurred in transmission/reception

瑕疵 2024-07-16 17:03:01

根据我的经验,手动计算时将其转换为多项式会更容易,尤其是当有很多零时。

1010 = 1*x^3 + 0*x^2 + 1*x^1 + 0*x^0 = x^3 + x = x3 + x
101101000 = x8 + x6 + x5 + x3

       -------------------
x3 + x ) x8 + x6 + x5 + x3

然后将被除数中的最大项 (x^8) 除以第一项< /strong> 除数 (x^3),结果为 x^5。 您将该数字放在顶部,然后它与除数中的每一项。 第一次迭代会产生以下结果:

        x5
       -------------------
x3 + x ) x8 + x6 + x5 + x3
         x8 + x6

对每一项进行异或,然后产生新的被除数:x5 + x3

        x5
       -------------------
x3 + x ) x8 + x6 + x5 + x3
         x8 + x6
       -------------------
         x5 + x3

遵循相同的模式,直到被除数的最大项小于除数的最大项。 计算完成后,将如下所示:

        x5 + x2
       -------------------
x3 + x ) x8 + x6 + x5 + x3
         x8 + x6
       -------------------
         x5 + x3
         x5 + x3
       -------------------
         0

本例中的提醒为 0,这表明传输过程中很可能没有发生错误。

注意:在上面的示例中,我将 x^y 缩短为 xy,以减少答案中的混乱,因为 SO 不支持数学方程格式。

注2:从被除数中添加/减去除数的倍数也会给出提醒 0,因为 (P(x) + a*C(x)) / C(x) = P(x)/C( x) + a*C(x)/C(x)P(x)/C(x) 给出相同的提醒,因为 a*C( 的提醒x)/C(x) 为 0。

In my experience it's easier to convert it to a polynomial when calculating by hand, especially when there're a lot of zeroes.

1010 = 1*x^3 + 0*x^2 + 1*x^1 + 0*x^0 = x^3 + x = x3 + x
101101000 = x8 + x6 + x5 + x3

       -------------------
x3 + x ) x8 + x6 + x5 + x3

Then you divide the largest term in the dividend (x^8) with the first term in the divisor (x^3), resulting in x^5. You put that number on top and then multiply it with each term in the divisor. This yields the following for the first iteration:

        x5
       -------------------
x3 + x ) x8 + x6 + x5 + x3
         x8 + x6

Doing XOR for each term then yields the new dividend: x5 + x3:

        x5
       -------------------
x3 + x ) x8 + x6 + x5 + x3
         x8 + x6
       -------------------
         x5 + x3

Follow the same pattern until the dividend's largest term is smaller then the divisor's largest term. After the calculations are complete it will look like this:

        x5 + x2
       -------------------
x3 + x ) x8 + x6 + x5 + x3
         x8 + x6
       -------------------
         x5 + x3
         x5 + x3
       -------------------
         0

The reminder in this case is 0, which would indicate that most likely no errors has occurred during the transmission.

Note: I've shortened x^y as xy in the example above to reduce the clutter in the answer, since SO doesn't support math equation formatting.

Note2: Adding/subtracting a multiple of the divisor from the dividend will also give the reminder 0, since (P(x) + a*C(x)) / C(x) = P(x)/C(x) + a*C(x)/C(x) gives the same reminder as P(x)/C(x) since the reminder of a*C(x)/C(x) is 0.

多情出卖 2024-07-16 17:03:01

它是除以二进制 11 的长除法。 Wikipedia 上有一个示例。

It is long division by binary 11. There is an example on Wikipedia.

小姐丶请自重 2024-07-16 17:03:01

假设我们要将 101110000 除以 1001。

101110000
1001
--------- XOR the 1011 and 1001
0010

现在我们将删除 XOR 结果开头的零(即 0010)并从顶部滑动数字

101110000
1001
--------- 
  1010

继续对结果执行 XOR 1001。

101110000
1001
--------- 
  1010
  1001
---------
  0011
--------- Remove zeros at the beginning
    1100
    1001
---------
    0101
--------- Remove zeros at the beginning
     1010
     1001
---------
     0011

答案是0011。

Let's assume that we want to divide 101110000 to 1001.

101110000
1001
--------- XOR the 1011 and 1001
0010

Now we will remove the zeros at the beginning of our XOR result which is 0010 and slide numbers from the top.

101110000
1001
--------- 
  1010

Continue the XOR 1001 with the result.

101110000
1001
--------- 
  1010
  1001
---------
  0011
--------- Remove zeros at the beginning
    1100
    1001
---------
    0101
--------- Remove zeros at the beginning
     1010
     1001
---------
     0011

Answer is 0011.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文