Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 与重置的计数器进行比较_C++_C_Logic_Counter - Fatal编程技术网

C++ 与重置的计数器进行比较

C++ 与重置的计数器进行比较,c++,c,logic,counter,C++,C,Logic,Counter,我有一个uint32柜台。我阅读这个计数器并将其存储为startCount=counter 然后我做一些操作,然后再次检查计数器。若计数器大于startCount+1(表示计数器必须至少递增两次) 现在,计数器达到unit32的最大值后将重置为零。 为了补偿这一点,我添加了 if (startCount == Max) if (counter > 0) break; else if (counter > startCount +1) || (counter <

我有一个uint32柜台。我阅读这个计数器并将其存储为
startCount=counter
然后我做一些操作,然后再次检查计数器。若计数器大于startCount+1(表示计数器必须至少递增两次)

现在,计数器达到unit32的最大值后将重置为零。 为了补偿这一点,我添加了

if (startCount == Max)
  if (counter > 0)
    break;
else
  if (counter > startCount +1) || (counter < startCount)
    break;
if(startCount==Max)
如果(计数器>0)
打破
其他的
如果(计数器>开始计数+1)| |(计数器<开始计数)
打破
我的问题是:有没有更好/更聪明的方法?
谢谢你的帮助。

只要检查下面的内容就可以了

if (counter > startCount +1) || ( (counter != startCount) && (counter < startCount +1)) 
    break;
if(计数器>开始计数+1)| |((计数器!=开始计数)&&(计数器<开始计数+1))
打破
`我会用

if (counter-startCount > 1) break;
或者,如果我想允许
int
具有超过32位

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

增量的数量计算模232。

您可以同时使用
uint64\u t
,但执行模运算
UINT23\u MAX
。您还可以注意到,即使
startCount==UINT23\u MAX
计数器>0
仍将等同于
计数器>startCount+1
。但总的来说,给出“如何做更聪明的事情”的建议需要你首先说明你为什么这么做。计数器是内部的,我不能更改类型。我不太擅长位比较,考虑到一些经验丰富的程序员可能会遇到类似的问题,我想知道这段代码是否可以简化为一条语句。当然,这是假设至少有一个增量。。在你的情况下可能不是最佳的?
if ((uint32_t)(counter-startCount) > 1) break;