Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

Re: [AVR-Chat] Bit fields

2010-06-08 by David Kelly

On Jun 7, 2010, at 4:25 PM, David VanHorn wrote:

> I have an application that needs to receive a variable number of
> packets in a block of data.
> I store them into a block-sized array with a packet-sized offset
> multiplied by the packet number..
> I need to know when I've received all the packets, but out-of-order
> and duplicate packets are possible.
> 
> I was thinking of doing this with a bit-field but that seems pretty messy.
> Are there other good ways to know when I've received all the packets,
> given that they may arrive out of order?


I think it would be best to pay the 8x memory penalty and allocate an array of uint8_t "received" flags than to use a bit array. But perhaps you have more available CPU cycles and FLASH code space than SRAM, then the bit array would be attractive.

I don't think an array of bit fields will work because C can not take the address of a bit.

A bit of Googling found some macros to perform bit operations on an array of uint8_t's. This is pretty much the way I was thinking of doing it, use divide to index into the array, then use modulo to determine which bit:

> #include <limits.h> /* for CHAR_BIT */
> 
> #define BITMASK(b) (1 << ((b) % CHAR_BIT))
> #define BITSLOT(b) ((b) / CHAR_BIT)
> #define BITSET(a, b) ((a)[BITSLOT(b)] |= BITMASK(b))
> #define BITTEST(a, b) ((a)[BITSLOT(b)] & BITMASK(b))
> 
> (If you don't have <limits.h>, try using 8 for CHAR_BIT.)

--
David Kelly N4HHE, dkelly@HiWAAY.net
========================================================================
Whom computers would destroy, they must first drive mad.

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.