Qu’est-ce qu’une bitmap en C?

Un bitmap est un tableau de bits . Comment est-il implémenté en C?

Je suppose que vous demandez comment implémenter un bitmap (ou un tableau de bits) en C. Étonnamment, l’entrée de Bit_array sur Wikipédia décrit le concept, mais n’indique pas réellement comment implémenter les opérations fondamentales.

En bref, faites un tableau de votre type non signé favori et faites l’arithmétique appropriée pour décider comment définir / effacer un peu le contenu.

#include  /* for CHAR_BIT */ #include  /* for uint32_t */ typedef uint32_t word_t; enum { BITS_PER_WORD = sizeof(word_t) * CHAR_BIT }; #define WORD_OFFSET(b) ((b) / BITS_PER_WORD) #define BIT_OFFSET(b) ((b) % BITS_PER_WORD) void set_bit(word_t *words, int n) { words[WORD_OFFSET(n)] |= (1 << BIT_OFFSET(n)); } void clear_bit(word_t *words, int n) { words[WORD_OFFSET(n)] &= ~(1 << BIT_OFFSET(n)); } int get_bit(word_t *words, int n) { word_t bit = words[WORD_OFFSET(n)] & (1 << BIT_OFFSET(n)); return bit != 0; }