このコードを作成して、最初のビットを削除し、次の要素からビットを追加しました。ここでの問題は、発生するすべてのシフトのために、8 番目の要素ですべてゼロになることです。このコードは醜いですが、うまくいくと思います。誰かがこれを行うためのより良い方法を提案できるかどうか、そして8要素ごとに発生するゼロ要素をどのように削除するかを考えていました。
前もって感謝します。
注* 6 週間しかコーディングしていません :P
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
unsigned char copy;
int i, j, n;
int shiftright;
int shiftleft;
shiftright = 6;
shiftleft = 2;
int counter = 0;
printf("Enter a number of values to test: ");
scanf("%d", &n);
unsigned char* array = malloc(n * sizeof(unsigned char));
copy = 0b01111111;
printf("Initial Array:\n");
for (i = 0; i < n; i++)
{
array[i] = copy;
printf("%x ", array[i]);
}
printf("\n");
// magic starts happening here
i = 0;
array[i] <<= 1;
for (j = 0; j < n; j++)
{
// counter to check for the 8th element
if (counter == 7)
{
counter = 0;
j++;
array[j] <<= 1;
}
counter++;
printf("sweep: %d\n", j);
// bitwise operations to remove zeros and append bits together
for (i = j; i < j + 1; i++)
{
if (array[i] == 0)
{
i++;
j++;
}
copy = array[i + 1];
copy >>= shiftright;
array[i] |= copy;
array[i + 1] <<= shiftleft;
shiftright--;
shiftleft++;
if (shiftright == -1)
{
shiftright = 6;
}
if (shiftleft == 9)
{
shiftleft = 2;
}
for (i = 0; i < n; i++)
{
printf("%x ", array[i]);
}
}
printf("\n");
}
return 0;
}