如何检测在C语言的包装计数器和较大的负值之间的差 [英] How to detect the difference between a wrapping counter and large negative value in C language

查看:233
本文介绍了如何检测在C语言的包装计数器和较大的负值之间的差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的愚蠢道歉,因为这是我在这个论坛上的第一篇文章。我试图发现一个包裹的无符号32位计数器,并与下面code,但编译器的帮助下,大的负跳之间的差异给我的错误:

Apologies for my imbecility as this is my first post on this forum. I am trying to detect the difference between a wrapping unsigned 32-bit counter and a large negative Jump with the help of following code but the compiler give me error:

错误:比较始终是真实的,由于数据类型范围限制[-Werror =类型限制]

下面是我的code片断:

Here is my code snippet:

#define MAX_BACKWARD_JUMP -4294959295 //UINT_MAX - 8000
#define MIN_BACKWARD_JUMP -3600
#define MAX_FORWARD_JUMP   4800000

signed int rtpDelta; //Signed 32-bit
unsigned int currRTPTs, prevRTPTs; //unsigned 32-bit

rtpDelta = currRTPTs - prevRTPTs;

  if ((rtpDelta > MAX_BACKWARD_JUMP && rtpDelta < MIN_BACKWARD_JUMP) 
        || (rtpDelta > MAX_FORWARD_JUMP))
        {
          printf("Delta in Timestamps too large\n",rtpDelta);
        }

这里的想法是捕捉在RTP时间戳无效的大三角洲。我们有一个当前时间戳和同行RTP客户端收到previous时间戳。对于RTP时间戳的无效值的边界限制为-4294959295&LT; rtpDelta&LT; -3600这是它应该抛出一个错误如果增量小于-3600和大于-4294959295,因为该值接近UMAX_INT将被视为翻车。我在做什么错在这里?

The idea here is to catch the invalid large Deltas in RTP timestamps. We have a current TimeStamp and a previous Timestamp receiving from peer RTP client. The boundary limits for invalid values of RTP Timestamps are -4294959295 < rtpDelta < -3600 that is it should throw an Error if the Delta is less than -3600 and greater than -4294959295 because the values closer to UMAX_INT will be considered as roll-over. What am I doing wrong here?

推荐答案

考虑:

unsigned int LowerBound = -3600u, UpperBound = 4800000u;

unsigned int difference = currRTPTs - prevRTPTs;

注意的是,由于包装,下界 -3600u 的价值,将是一个很大的正整数。现在,当数学上的差异(没有溢出来计算)小于-3600一个合理的量,差值将是一个大的整数,这将是小于下界。此外,如果差异不会成为太大(负方向),那么区别仍将比 UPPERBOUND 更大

Observe that, due to wrapping, the value of LowerBound, -3600u, will be a large positive integer. Now, when the mathematical difference (calculated without overflow) is less than -3600 by a reasonable amount, the value of difference will be a large integer, and it will be less than LowerBound. Additionally, if the difference does not become too great (in the negative direction), then difference will remain greater than UpperBound.

类似地,如果该差值大于4800000大于由合理数量,差的值将大于 UPPERBOUND 。如果差异不会成为太大时,则它仍将比下界少。

Similarly, if the difference is greater than 4,800,000 by a reasonable amount, the value of difference will be greater than UpperBound. If the difference does not become too much greater, then it will remain less than LowerBound.

因此​​,在这两种情况下,的值时,数学差是所希望的范围之外(但不是太多)小于下界和大于 UPPERBOUND

Thus, in both cases, the value of difference when the mathematical difference is outside the desired bounds (but not by too much) is less than LowerBound and greater than UpperBound:

if (difference < LowerBound && difference > UpperBound)
    printf("Delta in timestamps is outside acceptable bounds.\n");

注意,当数学差超过这将失败 -3600u (这是4,294,967,296 - 3600)或小于480万 - 4,294,967,296。因此,测试工作时,不同的是在[-4,290,167,296,4294963696]

Observe that this will fail when the mathematical difference exceeds -3600u (which is 4,294,967,296 - 3600) or is less than 4,800,000 - 4,294,967,296. Thus, the test works when the difference is in [-4,290,167,296, 4,294,963,696].

这篇关于如何检测在C语言的包装计数器和较大的负值之间的差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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