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

查看:22
本文介绍了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:

错误:由于数据类型范围有限[-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
",rtpDelta);
        }

这里的想法是在 RTP 时间戳中捕获无效的大增量.我们有从对等 RTP 客户端接收的当前时间戳和以前的时间戳.RTP 时间戳的无效值的边界限制为 -4294959295

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.

因此,在这两种情况下,当数学差异超出所需范围(但不是太多)时,difference 的值小于 LowerBound 且大于上界:

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.
");

请注意,当数学差异超过 -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天全站免登陆