bitfield array
Is it possible to declare a bitfield array?
I tried with
typedef struct
{
u8_t busy[10]:1;
}dummy;
but I obtain an error:
error: bit-field `busy' has invalid type
I also tried with
typedef struct
{
u8_t busy:1;
}dummy;
dummy a[10];
but then a is as large as 10 bytes.
thanks
Signature · 3 lines
Massimiliano Cialdi [email protected] [email protected]
Hi Massimiliano Cialdi,
Quoted 13 lines · On Mon, 12 Jan 2004 11:10:16 +0100 Massimiliano Cialdi <[email protected]>…
Is it possible to declare a bitfield array?
I tried withtypedef struct
{
u8_tbusy[10]:1;
}dummy;but I obtain an error:
error: bit-field `busy' has invalid typeI also tried with
Quoted 7 lines
typedef struct
{
u8_tbusy:1;
}dummy;
dummy a[10];but then a is as large as 10 bytes.
AFAIK, bitfields are stored in memory block which can only be allocated with 1-byte minimum.. Try to allocate 1 bit in memory, sounds not possible :-).
IOW, if you use a 1-bit bitfield, it will still be require an amount of memory counted in bytes. That's partly why bitfields are not so used for optimization, 'cause following the compiler you don't gaim memory space not cpu time.
My 2cts.
Regards,
Signature · 1 line
wwp
Quoted 3 lines · On Mon, 12 Jan 2004 11:29:26 +0100 wwp <[email protected]> wrote:
AFAIK, bitfields are stored in memory block which can only be allocated with 1-byte minimum.. Try to allocate 1 bit in memory, sounds not possible :-).
I know but I don't want to allocate a single bit. Since I need an array of binary flag, I thought that the compiler could help me. 10 bits could be placed in 2 bytes. Else I need 10 bytes (80% of space lost).
Quoted 4 lines
IOW, if you use a 1-bit bitfield, it will still be require an amount of memory counted in bytes. That's partly why bitfields are not so used for optimization, 'cause following the compiler you don't gaim memory space not cpu time.
I work with a (very) limited memory embedded system. I need to save memory, no matter in cpu time.
thanks
Signature · 3 lines
Massimiliano Cialdi [email protected] [email protected]
Maybe you should use a 2-byte variable, and access each bit with a mask. In this case, if you only need 10 one bit flags, there will be 6 unused bits, but you may use those bits with future flags... Anyway...you will be using only 2 bytes...and not 10 bytes... you still gain some memory.
-----Mensaje original-----
De: [email protected] [mailto:[email protected]]En nombre de Massimiliano Cialdi
Enviado el: Lunes, 12 de Enero de 2004 07:57
Para: linux-c-programming
CC: wwp
Asunto: Re: bitfield array
Quoted 3 lines · On Mon, 12 Jan 2004 11:29:26 +0100 wwp <[email protected]> wrote:
AFAIK, bitfields are stored in memory block which can only be allocated with 1-byte minimum.. Try to allocate 1 bit in memory, sounds not possible :-).
I know but I don't want to allocate a single bit. Since I need an array of binary flag, I thought that the compiler could help me. 10 bits could be placed in 2 bytes. Else I need 10 bytes (80% of space lost).
Quoted 4 lines
IOW, if you use a 1-bit bitfield, it will still be require an amount of memory counted in bytes. That's partly why bitfields are not so used for optimization, 'cause following the compiler you don't gaim memory space not cpu time.
I work with a (very) limited memory embedded system. I need to save memory, no matter in cpu time.
thanks
--
Massimiliano Cialdi
[email protected]
[email protected]
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 1 line · Massimiliano Cialdi
Is it possible to declare a bitfield array?
No.
Quoted 9 lines
I also tried with
typedef struct
{
u8_tbusy:1;
}dummy;
dummy a[10];but then a is as large as 10 bytes.
Structures which contain bitfields will be padded to an integral number of bytes.
Quoted 7 lines
Quoted 3 lines
AFAIK, bitfields are stored in memory block which can only be allocated with 1-byte minimum.. Try to allocate 1 bit in memory, sounds not possible :-).
I know but I don't want to allocate a single bit. Since I need an array of binary flag, I thought that the compiler could help me. 10 bits could be placed in 2 bytes. Else I need 10 bytes (80% of space lost).
Declare a 16-bit field and set of macros to access the individual bits, e.g.
typedef struct
{
uint16_t bits;
} dummy;
#define get_flag(s, i) (((s).bits >> (i)) & 1)
#define set_flag(s, i) ((s).bits |= 1 << (i))
#define clear_flag(s, i) ((s).bits &= ~(1 << (i)))Signature · 1 line
Glynn Clements <[email protected]>
Quoted 7 lines
Quoted 5 lines
IOW, if you use a 1-bit bitfield, it will still be require an amount of memory counted in bytes. That's partly why bitfields are not so used for optimization, 'cause following the compiler you don't gaim memory space not cpu time.
I work with a (very) limited memory embedded system. I need to save memory, no matter in cpu time.
if this is the case you could create a macro / function to access the individtual bits.
It is slow but you dont have a problem with that
something like
int grab_bit(u8 *bits, int bit) {
return ((bits[bit >> 3] & (1 << (bit >> 3) & 255)) >> bit);
}void set_bit(u8 *bits, int bit) {
bits[bit >> 3] |= 1 << ((bit >> 3) & 255);
}I am pritty sure the above is wrong but you should get the rought idea you have an array of 8bits and you access by dividing by 8 then using the reminder value as the mask to access the individtual bits.
It should be possible todo it with shifts so you dont loose soo much speed. Then just use the functions with a declared array of bits.
James