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

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

问题描述

因为这是我在这个论坛上的第一篇文章,对我的无比的歉意。我试图检测一个包装无符号32位计数器和一个大负跳跃之间的差异借助下面的代码,但编译器给我错误:

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:

error:由于数据类型范围有限,比较总是为真[-Werror = type-limits]

error: comparison is always true due to limited range of data type [-Werror=type-limits]

这是我的代码片段:

#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时间戳中无效的大Deltas。我们有一个当前时间戳和一个先前的时间戳从对等RTP客户端接收。 RTP时间戳的无效值的边界限制为-4294959295< rtpDelta< -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;

注意,由于包装, LowerBound -3600u 将是一个大的正整数。现在,当数学差异(无溢出计算)小于-3600一个合理的量时, difference 的值将是一个大整数,它将小于 LowerBound 。此外,如果差异不会变得太大(负方向),则 difference 将保持大于 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.

类似地,如果差值大于4,800,000一个合理的量,则 difference 的值将更大 UpperBound 。如果差值没有变得太大,那么它将保持小于 LowerBound

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.

当数学差异在期望界限之外(但不是太多)时,差异的值小于 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),或小于4,800,000 - 4,294,967,296。因此,当差值在[-4,290,167,296,4,294,963,696]时,测试工作。

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天全站免登陆