C中的位字段结构数组

时间:2017-09-10 13:22:48

标签: c bit-fields

如何创建可变大小的位字段数组? 以下代码是我尝试过的,但没有用。

#include <stdio.h>
int main()
{
    int n=4;
    struct bite{
        unsigned a1:2;
        unsigned a2:2;
            :
            :
        unsigned a(n-1):2;
        unsigned a(n):2;
    }bits;

    for(i=1;i<=n;i++)
        bits.a[i]=i;

    for(i=1;i<=n;i++)
        printf("%d ",bits.a[i]);

    return 0;
}

3 个答案:

答案 0 :(得分:2)

无法在运行时定义struct的成员。

您可以使用char数组和一些宏来模拟位数组。

#define BitArray(array, bits) \
  unsigned char array[bits / 8 + 1]

#define SetBit(array, n) \
  do { array[n / 8] |= (1 << (n % 8)) } while (0)
#define GetBit(array, n) \
  ((array[n / 8] >> (n % 8)) & 1)

int main(void)
{
  BitArray(bits, 42); /* Define 42 bits and init to all 0s
                               (in fact this allocates memory for (42/8 + 1) * 8 bits). */

  SetBit(bits, 2); /* Set bit 2. */
  int bit2 = GetBit(bits, 2); /* Get bit 2 */

  ...

根据您的代码,类似于2位字:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define Define2BitWordArray(array, two_bit_words) \
  unsigned char array[two_bit_words / 4 + 1]

#define Set2BitWord(array, n, value) \
  do { array[n / 4] |= (unsigned char)((value & 0x11) << (2 * (n % 4))); } while (0)
#define Get2BitWord(array, n) \
  ((array[n / 4] >> (2 * (n % 4))) & 0x11)

int main(void)
{
  size_t n = 10;
  Define2BitWordArray(bits, n); /* Define 10 two-bits words
                                  (in fact this allocates memory 
                                  for (10/4 + 1) * 4 two-bit bits). */
  memset(bits, 0, sizeof bits); /* Set all bits to 0. */

  for(size_t i = 0; i < n; i++) /* C array's indexes are 0-based. */
  {
    Set2BitWord(bits, i, i);
  }

  for(size_t i = 0; i < n; i++)
  {
    printf("%d ", Get2BitWord(bits, i));
  }
}

答案 1 :(得分:0)

请记住,您的数组始终从0位置开始。

你可以这样做:

#include <stdio.h>

typedef struct
{
    unsigned int x : 1;
} one;

int main() {
    int n = 10;
    one xx[n];
    int i;

    for(i=0;i<n;i++)
       xx[i].x=i;
    for(i=0;i<n;i++)
       printf("%d ",xx[i].x);

    return 0;
}

答案 2 :(得分:0)

如果你想对没有限制大小的数据上的特定位或位进行操作(实际上限于size_t的最大值除以8)。这个例子中的小端

void setbit(void *obj, size_t bit)
{
    uint8_t *p = obj;

    p[bit >> 3] |= (1 << (bit & 7));
}

void resetbit(void *obj, size_t bit)
{
    uint8_t *p = obj;

    p[bit >> 3] &= ~(1 << (bit & 7));
}

void assignbit(void *obj, size_t bit, unsigned value)
{
    uint8_t *p = obj;

    p[bit >> 3] &= ~(1 << (bit & 7));
    p[bit >> 3] != (!!value << (bit & 7));
}

void setbits(void *obj, size_t bit, size_t num)
{
    while (num--)
        setbit(obj, bit + num);
}

void resetbits(void *obj, size_t bit, size_t num)
{
    while (num--)
        resetbit(obj, bit + num);
}


void assignbits_slow(void *obj, size_t pos, size_t size, unsigned value) 
{
    for (size_t i = 1, j = 0; j < size; j++, i <<= 1)
        assignbit(obj, pos + j, !!(value & i));
}

int getbit(void *obj, size_t bit)
{
    uint8_t *p = obj;

    return !!(p[bit >> 3] & (1 << (bit & 7)));
}
void *getbits(void *obj, void *buff, size_t bit, size_t nbits)
{
    memset(buff, 0, nbits >> 3 + !!(nbits & 7));

    do
    {
        nbits--;
        assignbit(buff, bit + nbits, getbit(obj, bit + nbits));
    } while (nbits);
    return buff;
}