gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 03:18:49 25 4
gpt4 key购买 nike

我目前有存储 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com