-
typedef struct problem
Code:
typedef struct
{
unsigned char
b0:1,b1:1,b2:1,b3:1,b4:1,b5:1,b6:1,b7:1;
}
bitset;
So I got a struct consisting of a char with 8x1bit identifiers.
For example I'll make 50 of them.
Code:
bitset* collection = calloc(50, 1);
Now giving each char binary data.
Code:
for (i = 0; i < 50; i++)
{
collection[i].b0 = 1; collection[i].b1 = 0;
collection[i].b2 = 0; collection[i].b3 = 1;
collection[i].b4 = 1; collection[i].b5 = 0;
collection[i].b6 = 1; collection[i].b7 = 1;
}
The values aren't irrelevant, I just want to know if there's a way of looping through the bits to manipulate them. Here's a concept snippet:
Code:
for (i = 0; i < 50; i++)
{
for (j = 0; j < 8; j++)
{
collection[i].j = 1;
}
}
That's obviously not the way to go but do you think it involves naming the identifiers something else?
Please enlighten me fellow programmers :)
-
Is there a need to be one? :)
If you want some additional feature: built a function yourself.
Code:
SetBitset(bitset* b, int b0, int b1, int b2, ...)
{
int i;
for (i = 0; i < 8; ++i)
{
b.b0 = b0;
...
}
}
If you want to make a function that sets everything to something you can do so as well.
You could try this:
Code:
struct bitset
{
....
}
typedef union
{
struct bitset bits;
unsigned char value;
} Bitset;
...
Bitset a;
a.value = 0xFF; //all bits to 1
a.bits.b0 = 0; //bit 0 to 0
Not sure if you can use a struct inside a union. If you can then in this way you can manipulate the bits of a byte more easily which is probably what you want.
-
cheers
thanks very much it's just what I needed :)