Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error trying to define a 1,024-bit (128 Byte) Bit Field

Tags:

c

bit-fields

I would like to define a large bitfield for the purpose of quickly monitoring the status a very large structure of elements. Here is what I have so far:

#define TOTAL_ELEMENTS 1021

typedef struct UINT1024_tag
{
   UINT8 byte[128];
} UINT1024;

typedef struct flags_tag
{
   UINT1024:TOTAL_ELEMENTS;
} flags_t;

When I try compiling this, I get the error message, "error: bit-field `<anonymous>' has invalid type"

Can bit-fields only be used on certain types? I thought that if I defined a large-enough variable, the massive bitfield needed for my application could then be defined because the bitfield has to be no larger than the type used to define it.

Any thoughts or suggestions would be appreciated.

like image 789
Jim Fell Avatar asked Dec 17 '22 05:12

Jim Fell


1 Answers

Bit fields must fit within a single int, you can't use arbitrary sizes. Honestly the ANSI bitfield implementation is kinda broken. It misses a lot of other stuff too, like control over padding and layout that real-world applications usually need. I'd consider writing some macros or accessor functions to abstract the larger sizes and giving up on the bitfield syntax.

like image 144
Andy Ross Avatar answered Jan 28 '23 00:01

Andy Ross