简体   繁体   English

如何用整数中的任意位替换/覆盖short数组中任意位置的任意数量的位

[英]How to replace/overwrite an arbitrary number of bits in an arbitrary position in an array of short with arbitrary bits from an integer

I have a function I'm trying to write of the following form (and haven't found exactly what I'm looking for — if this is a dup please just point me at the right place — even if it's not int s and short s but, say, char s and int s instead, that would be fine): 我有一个要尝试以以下形式编写的函数(并且没有完全找到我要寻找的内容—如果这是一个重复项,请仅将我指向正确的位置—即使它不是intshort s,但是用char s和int代替,那就可以了):

put_bits(short *array_of_short, int significant_bits, int bit_offset, int integer_to_append)

Where I overwrite the the significant_bits of integer_to_append at bit_offset in array_of_short . 当我改写了significant_bitsinteger_to_appendbit_offsetarray_of_short

I'd like to accomplish things by just overwriting (or bitwise oring, or overlaying, or replacing) bits to the position in the array (I don't want to add more elements to the array or allocate more memory) — ie it should be easily possible, but pretty inefficient, to just keep track of how many elements into the array the offset translates to, whether this falls on a boundary of the shorts and shift the bits of the integer to the appropriate offset and or them onto the appropriate short(s) — but that seems like loads of overhead and calculating more than I need to vs just oring the bits into the appropriate spot, but I'm kind of at a loss... 我想通过仅覆盖(或按位排序,叠加或替换)位到数组中的位置来完成事情(我不想向数组中添加更多元素或分配更多内存),即可能很容易,但是效率很低,只是跟踪偏移量转换为数组中的元素数量,偏移量是否落在短裤的边界上,以及将整数位移至适当的偏移量或将其移至适当的偏移量短(s)—但这似乎是开销的负担,而且计算量超出了我需要的,而不仅仅是将位运算到适当的位置,但是我有点茫然……

So, for example, I have an integer which will contain an arbitrary number of "significant" bits — let's say for this example there are 6. So the values would be from 0 to 63 因此,例如,我有一个整数,其中将包含任意数量的“有效”位-假设在此示例中有6个。因此,值将为0到63

0000 0000 0000 0000 0000 0000 0000 0000

to

0000 0000 0000 0000 0000 0000 0011 1111

and I want to overlay (or bitwise or this) this to an arbitrarily sized array of short at an arbitrary point. 我想将其覆盖(或按位或此)到任意点的任意大小的short数组。 So if I had 所以如果我有

Integer: 整数:

0000 0000 0000 0000 0000 0000 0010 0001

Array of short: 短数组:

0100 1000 0100 1100 : 1100 0010 0110 0000 : 0000 0000 0000 0000 : 0000 0000 0000 0000

and I wanted to append at position 42 to get: 我想在位置42处追加:

0100 1000 0100 1100 : 1100 0010 0110 0000 : 0000 0000 0000 1000 : 0100 0000 0000 0000

If I'm totally off or I don't make sense, let me know too. 如果我完全不在家或者我没有道理,也请让我知道。

If i understand your question correctly, you actually want to treat your array as array of bits. 如果我正确理解了您的问题,您实际上想将您的数组视为位数组。 There is no such structure in c as bit array of course, but you can implement it. 当然,c中没有位数组这样的结构,但是您可以实现它。 Here is example of bit array with int as base type. 是以int为基本类型的位数组的示例。 You can adopt this solution with short as base type, and then just set bit by bit something like that: 您可以采用short作为基本类型的解决方案,然后逐个设置如下:

for( i = 0 ; i< sizeof(int)*8;++i)
{
       unsigned int flag = 1;
       flag = flag << i;
       if( int_num & flag)
           SetBit( array_of_short, bit_offset + i ); 
}

void  SetBit( short array_of_short[ ],  int k )
{
   array_of_short[k/16] |= 1 << (k%16);  // Set the bit at the k-th position in  array_of_short[i]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM