c - 删除存储在数组中的标志位

标签 c arrays loops

我目前有存储 01111110 0110111 01111110 的发射器阵列 我希望接收器数组存储 0110111。我想消除所有 01111110 位。

但我收到的是 0110111 01111110。为什么我的代码只删除了发射器阵列中的前 01111110 位?

我尝试的代码如下:

#define nosbits 23
#include<stdio.h>
#include <stdlib.h>
#include<stdbool.h> 

int main()
{

   unsigned transmitter[nosbits] = { 0,1,1,1,1,1,1,0,0,1,1,0,1,1,1,0,1,1,1,1,1,1,0 };
   unsigned receiver[nosbits];
   int count = 0;
   int outputreceivercount = 0;
   bool flag = false;

   for (int i = 0; i < nosbits; i++)
   {

       if (transmitter[i] == 1)
       {
          count++;
       }
       else
       {
          count = 0;
       }

       receiver[outputreceivercount++] = transmitter[i];

       //After 5 consecutive 1s, if the next two bits are '10', then the flag is detected. 
       if ((transmitter[i + 1] == 1) && (transmitter[i + 2] == 0) && count == 5)
       {
           if (!(flag))
           {
              flag = true;
              i = i + 2;
              outputreceivercount = 0;
              count = 0;
           }  
        }
   }


   printf("Bitstream before removing flag bits:\n");
   for (int i = 0; i < nosbits; i++)
   {
      printf("%d", transmitter[i]);
   }
   printf("\n\n");


   printf("Bitstream after removing flag bits:\n");
   for (int i = 0; i < outputreceivercount; i++)
   {
      printf("%d", receiver[i]);
   }
   printf("\n");

   system("pause");
   return 0; 

}

最佳答案

实际上,您不需要“flag”变量来检测标志。 替换以下代码,看看是否有帮助。

//After 5 consecutive 1s, if the next two bits are '10', then the flag is detected. 
   if ((transmitter[i + 1] == 1) && (transmitter[i + 2] == 0) && count == 5)
   {                        
        i = i + 2;      
        outputreceivercount -= (count+1);
        count = 0;      
    }

如果需要,可以进一步优化。

关于c - 删除存储在数组中的标志位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48614098/

相关文章:

c - 处理器本身如何假定任何程序的执行时间?

c - 使用动态数组时增加内存

php - 将数组分组为 2 个元素的子数组,最后一组缺失

c++ - 通过 move 赋值实现 B=f(A) 语法

python - django 只打印一次循环值

java - 批处理逻辑错误?

c++ - 关于优化的数学函数、范围和区间

c++ - 在 C++ 中编译 C 库。链接问题。穆PDF

php - array_unique() 没有排序

java - 如何比较数组元素并将相应元素添加到第三个数组?