DavS
DavS

Reputation: 43

C bit field/array

I have to do a project for school. But I got stuck right at the beginning.

I have to define type for bitfield. It isn't a problem, it would look like this:

typedef struct {
   unsigned flag1 : 1;
   unsigned flag2 : 1;
}BitArray;

But next task is to do set of macros for working with bitfield. One of them is:

create(field_name,size) /* defines and initializes bitfield */

The question is how can I typedef bitfield so I could change number of its members later?

Second method that came to my mind is to use bool array. But again, how can I typedef bool array? At frist I tried:

typedef bool BitArray[]; //new identifier BitArray for bool array
BitArray Array[5];      //new BitArray variable Array - this line would be in the macro mentioned above

It didn't take me a long time to realise that it won't work. However I can do:

typedef bool BitArray;
BitArray Array[5];

But it just ranames identifier for bool.

I hope my post makes sense and thank you for any advise you can give.

Upvotes: 1

Views: 797

Answers (2)

tonso
tonso

Reputation: 1768

In C you can declare a typedef only for fixed-size array, like this:

typedef bool bitset8[8]; // 8 is constant expression
bitset8 bs8;
bs8[0] = true;

I don't quite understand how exactly create macro from your post is going to be used, but if you need to dynamically change number of fields you have to use malloc'ed objects ANYWAY, so the declaration of BitArray struct should contain a pointer to let's say unsigned char (that is a pointer to byte array essentially). The content of the array should be managed by separate functions, that may be called from macros (though there is no real need in them).

Upvotes: 1

Lundin
Lundin

Reputation: 213286

The question is how can I typedef bitfield so I could change number of its members later?

That's what the typedef struct ... BitArray does. No need for additional types.

A bool array will most likely not compile into bit-wise code, it will likely compile into an array of bytes, which is not what you want.

In addition, it is a very bad idea to hide arrays or pointers behind typedefs, so that they don't look like arrays or pointers no longer.


Some recommendations:

I have to define type for bitfield

I would not recommend to use bit fields for any purpose what-so-ever. You should question your teacher why they are teaching you to use dangerous and poorly specified parts of the C language.

But next task is to do set of macros for working with bitfield

You should ask your teacher why they teach you to use function-like macros and not proper functions. Using a function-like macro might be the worst thing you can ever do in C: they are dangerous, they are unreadable, they are hard to debug, they are hard to maintain.

Combining function-like macros mixed with bit-fields seems like a really stupid idea, but of course that is just my personal opinion. The safe and 100% portable way is to use bit-wise operators with masks on byte-level variables, such as:

uint8_t my_var=0;
my_var |= 0x80; // set msb, bit 7
my_var &= ~0x80; // clear msb, bit 7

Upvotes: 2

Related Questions