简体   繁体   English

与重置的计数器相比

[英]Comparing to a counter which reset

I have an uint32 counter. 我有一个uint32计数器。 I read this counter and store it like startCount = counter; 我读了这个计数器并像startCount = counter;一样存储它startCount = counter; Then I do some operations and then check again the counter. 然后,我进行一些操作,然后再次检查计数器。 If counter is greater than startCount+1 (means counter must be incremented at least twice). 如果counter大于startCount + 1(意味着counter必须至少增加两次)。

if (counter > startCount +1 )
  break;

Now the counter will be reseted to zero once it reaches max value for unit32. 现在,一旦达到unit32的最大值,计数器将重置为零。 To compensate this I have added 为了弥补这一点,我添加了

if (startCount == Max)
  if (counter > 0)
    break;
else
  if (counter > startCount +1) || (counter < startCount)
    break;

My question is: is there a better / smarter way to do that? 我的问题是:有没有更好/更聪明的方法呢? Thanks for all the help. 感谢您的所有帮助。

Just checking this below should cover everything 只需在下面检查即可涵盖所有内容

if (counter > startCount +1) || ( (counter != startCount) && (counter < startCount +1)) 
    break;

` `

I'd use 我会用

if (counter-startCount > 1) break;

or, if I'd want to allow for int having more than 32 bits, 或者,如果我想让int位数超过32位,

if ((uint32_t)(counter-startCount) > 1) break;

the number of increments computes modulo 2 32 . 增量的数量计算模2 32

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM