Цитата(Ph. Anatoliy @ Oct 1 2008, 14:15)

Вот Вам, мой вариант:
#define BUFFER_BIT_SIZE 32
unsigned char buffer[BUFFER_BIT_SIZE/8+1]; // +1 для запаса. Если буфер кратен 8 то не нужно
А вот Вам мой:
Код
unsigned char buffer[(BUFFER_BIT_SIZE + 7)/8];//всегда выделяет столько, сколько нужно
И можно оформить это в виде макросов:
Код
#ifndef BIT_ARRAY_H__
#define BIT_ARRAY_H__
#include <stdint.h>
#define BA_BASE uint8_t // 8 bits
#define BA_BASE_BITS (CHAR_BIT * sizeof(BA_BASE)) // 8
#define BA_BASE_MASK (BA_BASE_BITS - 1) // 7 = 0x07
#define BIT_ARRAY(name, size) \
BA_BASE name[ (size + BA_BASE_BITS - 1) / BA_BASE_BITS ]
// Macro can be modified to functions to reduce code size
#define BIT_SET(array, bit) \
do { array[ bit / BA_BASE_BITS ] |= (1 << ( bit & BA_BASE_MASK)); } while (0)
#define BIT_CLR(array, bit) \
do { array[ bit / BA_BASE_BITS ] &= ~(1 << ( bit & BA_BASE_MASK)); } while (0)
#define BIT_TEST(array, bit) \
( array[ bit / BA_BASE_BITS ] & (1 << ( bit & BA_BASE_MASK)) )
/*
// functions can be placed into separate .c file to reduce code size
void BIT_SET(BA_BASE *array, uint8_t bit)
{
array[ bit / BA_BASE_BITS ] |= (1 << ( bit & BA_BASE_MASK));
}
void BIT_CLR(BA_BASE *array, uint8_t bit)
{
array[ bit / BA_BASE_BITS ] &= ~(1 << ( bit & BA_BASE_MASK));
}
BA_BASE BIT_TEST(BA_BASE *array, uint8_t bit)
{
return array[ bit / BA_BASE_BITS ] & (1 << ( bit & BA_BASE_MASK));
}
*/
#endif //BIT_ARRAY_H__
использование:
BIT_ARRAY(Array1, 50);
BIT_ARRAY(Array2, 16);
void test()
{
BIT_SET(Array1, 45);
if(BIT_TEST(Array1, 27))
BIT_CLR(Array2, 5)
}