如何操作数组的单个位

时间:2018-05-22 16:53:05

标签: c

所以我在头文件中定义了一个带有无符号字符数组的结构。

#ifndef BITSTREAM_H
#define BITSTREAM_H

typedef struct {
int lengthofarray;
int position;
unsigned char* arrayofunsignedchar;
}Bitstream;

#endif // INTARRAY_H

我在int main函数中创建了包含长度为8的数组的结构。并在create函数中将字段的所有位设置为0。但是bitmanipulation函数是我在努力的地方。

int main(void)
{
Bitstream * bs = malloc(sizeof(Bitstream));
create(bs, &length);
destroy(bs);
bs = malloc(sizeof(Bitstream));
create(bs, &length);      
bitmanipulation(bs, 4, 4);
bitmanipulation(bs, 9, 27);

return EXIT_SUCCESS;
}

现在我想编写函数bitmanipulation来将此数组的给定位数设置为给定值。我知道我必须按位和&或按位OR | 。但是我不确定,例如,如何只对我的第一个数组元素的最后3位进行地址处理。我想我必须将它按位与无符号整数比较,前5位为0但后来我不知道如何在函数内设计它以适应每个给定数量的位和值。如果有人可以举例说明如何操作数组元素的一部分,那将是非常棒的。

void bitmanipulation(Bitstream * bs, int numberofbits, int valueofbits){
//printf("%d position\n", (*bs).position);
//here should be the code to change the "numberofbits" bits before 
to be represent the valueofbits.
if((*bs).position == 0){
(*bs).position = (numberofbits-1);
}else {
(*bs).position = (*bs).position + numberofbits;
}
printf("%d position\n", (*bs).position);
printf("%d value of the element\n", (*bs).arrayofunsignedchar[0]);

}

bitmanipulation(bs, 4, 4);
// this should only manipulate the first 4 bits in the array 
arrayofunsignedchar and only set bit 2 to be 1. So Bit 0;1;3 would still
be 0 and they would be 0 from now on. And it updates the position to be 3;


bitmanipulation(bs, 9, 27);
//this now would have to manipulate bit 4 to bit 12; So It 
would set bit 4, bit 5, bit 7, bit 8; and bit 6;9;10;11;12 
would be 0 from now on. And it updates the position to be 12;

因此它会将arrayofunsigned char视为一个位域。在第一次进行位操作的函数调用之后,前4位将代表值4.然后第二个f1调用将操作位域中的下一个9位。并且代表值27.等等。例如 bitmanipulation(bs,2,0)只会执行任何操作,因为第13位和第14位已经为0。如果我现在称为bitmanipulation(bs,2,2),它会将第16位改为1,所以这2位" 15"和" 16"一起代表数字1。

在我调用bitmanipulation之后,我最后一次打印由一行无符号整数表示的arrayofunsignedchar。在这个例子中它将是

arrayofunsignedchar[0] = 180 (00101101)
arrayofunsignedchar[1] = 1 (10000000)
arrayofunsignedchar[2] = 1 (10000000)

所以打印输出为180; 1; 1; 0; 0; 0; 0; 0。

2 个答案:

答案 0 :(得分:0)

考虑到这一点 4是二进制100,而9是二进制1001

 void bitmanipulation(Bitstream * bs, const int numberofbits, const int valueofbits) {
     int write=valueofbits;
     for(int i=0;i<numberofbits;++i) {
         if(write%2==1) { 
             // put 1 bit at position+i in bs->arrayofunsignedchar
         }else{
             // put 0 bit at position+i in bs->arrayofunsignedchar
         }
         write /= 2;
     }
     ...
 }

1放在n数组unsigned char中需要您计算要写入的unsigned char(假设unsigned charvoid writeBit(unsigned char* output,const int position,const int value) { int charIndex=position/8; int bitInCharIndex=position%8; unsigned char input= 1; input <<= bitInCharIndex; if(value==1) { output[charIndex] |= input; return; } // else 0 input = ~input; output[charIndex] &= input; } 8位长)

react-helmet
注意不要超过数组长度。

答案 1 :(得分:0)

感谢dvhh,我可以完成它。

void bitmanipulation(Bitstream * bs, const int numberofbits, const int valueofbits) {
    int k = valueofbits;
    if ((*bs).position == 0) {
    printf("%d position\n", (*bs).position);
    }
    for (int i = 0; i <numberofbits; i++){
    if ((*bs).position > ((8*(*bs).lengthofarray)-1)){
    return;
    }
    if (k%2==1) {
    int charIndex = (*bs).position/8;
    int bitInCharIndex = (*bs).position%8;
    unsigned int input = 1;
    input <<= bitInCharIndex;
    (*bs).arrayofunsignedchar[charIndex] |= input;
    }
    k = k/2;
    (*bs).position = (*bs).position + 1;
    printf("%d position\n", (*bs).position);
    } 
    }

    void printthearray(Bitstream * bs){
    for (int i = 0; i < (*bs).lengthofarray; i++){
    unsigned char t = (*bs).arrayofunsignedchar[i];
    printf("%u ist das %dte Element des Arrays als Ganzzahl\n", t, i);
    }
    }

    int main(void)
    {
    Bitstream * bs = malloc(sizeof(Bitstream));
    create(bs, 10);
    destroy(bs);
    bs = malloc(sizeof(Bitstream));
    create(bs, 10);
    bitmanipulation(bs, 4, 4);
    bitmanipulation(bs, 9, 27);
    bitmanipulation(bs, 6, 27);
    bitmanipulation(bs, 4, 4);
    bitmanipulation(bs, 9, 27);
    bitmanipulation(bs, 6, 27);
    bitmanipulation(bs, 4, 4);
    bitmanipulation(bs, 9, 27);
    bitmanipulation(bs, 6, 27);
    bitmanipulation(bs, 4, 4);
    bitmanipulation(bs, 9, 27);
    bitmanipulation(bs, 6, 27);
    bitmanipulation(bs, 4, 4);
    bitmanipulation(bs, 9, 27);
    bitmanipulation(bs, 6, 27);
    bitmanipulation(bs, 4, 4);
    bitmanipulation(bs, 9, 27);
    bitmanipulation(bs, 6, 27);
    bitmanipulation(bs, 6, 27);
    bitmanipulation(bs, 4, 4);
    bitmanipulation(bs, 9, 27);
    bitmanipulation(bs, 6, 27);
    bitmanipulation(bs, 4, 4);
    bitmanipulation(bs, 9, 27);
    bitmanipulation(bs, 6, 27);
    bitmanipulation(bs, 4, 4);
    bitmanipulation(bs, 9, 27);
    bitmanipulation(bs, 6, 27);
    printthearray(bs);
    destroy(bs);

    return EXIT_SUCCESS;

    }

所以程序会打印

180 ist das 0te Element des Arrays als Ganzzahl 
// translated into English: 180 is the 0 element of the array 
// printed as an unsigned int.
97 ist das 1te Element des Arrays als Ganzzahl
163 ist das 2te Element des Arrays als Ganzzahl
13 ist das 3te Element des Arrays als Ganzzahl
27 ist das 4te Element des Arrays als Ganzzahl
109 ist das 5te Element des Arrays als Ganzzahl
216 ist das 6te Element des Arrays als Ganzzahl
104 ist das 7te Element des Arrays als Ganzzahl
195 ist das 8te Element des Arrays als Ganzzahl
70 ist das 9te Element des Arrays als Ganzzahl