* bitfield array
@ 2004-01-12 10:10 Massimiliano Cialdi
2004-01-12 10:29 ` wwp
2004-01-12 21:06 ` Glynn Clements
0 siblings, 2 replies; 6+ messages in thread
From: Massimiliano Cialdi @ 2004-01-12 10:10 UTC (permalink / raw)
To: linux-c-programming
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
--
Massimiliano Cialdi
cialdi@firenze.net
m.cialdi@oksys.it
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: bitfield array 2004-01-12 10:10 bitfield array Massimiliano Cialdi @ 2004-01-12 10:29 ` wwp 2004-01-12 10:56 ` Massimiliano Cialdi 2004-01-12 21:06 ` Glynn Clements 1 sibling, 1 reply; 6+ messages in thread From: wwp @ 2004-01-12 10:29 UTC (permalink / raw) To: linux-c-programming Hi Massimiliano Cialdi, On Mon, 12 Jan 2004 11:10:16 +0100 Massimiliano Cialdi <cialdi@firenze.net> wrote: > 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. 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, -- wwp ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: bitfield array 2004-01-12 10:29 ` wwp @ 2004-01-12 10:56 ` Massimiliano Cialdi 2004-01-12 13:40 ` Mariano Moreyra 2004-01-13 1:02 ` James Stevenson 0 siblings, 2 replies; 6+ messages in thread From: Massimiliano Cialdi @ 2004-01-12 10:56 UTC (permalink / raw) To: linux-c-programming; +Cc: wwp On Mon, 12 Jan 2004 11:29:26 +0100 wwp <subscript@free.fr> 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). > 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 cialdi@firenze.net m.cialdi@oksys.it ^ permalink raw reply [flat|nested] 6+ messages in thread
* RE: bitfield array 2004-01-12 10:56 ` Massimiliano Cialdi @ 2004-01-12 13:40 ` Mariano Moreyra 2004-01-13 1:02 ` James Stevenson 1 sibling, 0 replies; 6+ messages in thread From: Mariano Moreyra @ 2004-01-12 13:40 UTC (permalink / raw) To: 'Massimiliano Cialdi', 'linux-c-programming' Cc: 'wwp' 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: linux-c-programming-owner@vger.kernel.org [mailto:linux-c-programming-owner@vger.kernel.org]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 On Mon, 12 Jan 2004 11:29:26 +0100 wwp <subscript@free.fr> 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). > 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 cialdi@firenze.net m.cialdi@oksys.it - To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: bitfield array 2004-01-12 10:56 ` Massimiliano Cialdi 2004-01-12 13:40 ` Mariano Moreyra @ 2004-01-13 1:02 ` James Stevenson 1 sibling, 0 replies; 6+ messages in thread From: James Stevenson @ 2004-01-13 1:02 UTC (permalink / raw) To: Massimiliano Cialdi; +Cc: linux-c-programming, wwp > > 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 ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: bitfield array 2004-01-12 10:10 bitfield array Massimiliano Cialdi 2004-01-12 10:29 ` wwp @ 2004-01-12 21:06 ` Glynn Clements 1 sibling, 0 replies; 6+ messages in thread From: Glynn Clements @ 2004-01-12 21:06 UTC (permalink / raw) To: Massimiliano Cialdi; +Cc: linux-c-programming Massimiliano Cialdi wrote: > Is it possible to declare a bitfield array? No. > I also tried with > > typedef struct > { > u8_t busy: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. > > 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))) -- Glynn Clements <glynn.clements@virgin.net> ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2004-01-13 1:02 UTC | newest] Thread overview: 6+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2004-01-12 10:10 bitfield array Massimiliano Cialdi 2004-01-12 10:29 ` wwp 2004-01-12 10:56 ` Massimiliano Cialdi 2004-01-12 13:40 ` Mariano Moreyra 2004-01-13 1:02 ` James Stevenson 2004-01-12 21:06 ` Glynn Clements
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox