I am quite unfamiliar with operating on individual bits. This question is more of a sanity check than anything else:
I would like a list of, say, one million individual bits. To do this, I should create a list of 1000000/16 unsigned ints. Then, by iterating through these unsigned ints I can use bitwise operators to set the individual bits to the values I desire.
Is this correct, or am I being really dumb?
If I am on the right track, am I always guaranteed that an unsigned int will be 16 bits?
No, there’s no guarantee that
unsigned intwill be 16 bits wide. Useuint16_tinstead.When you say “list”, do you mean an array or a linked list? It almost certainly makes more sense to use an array than to use a linked list. It will lead to much better memory utilization, and will enable random access to individual words/bits.
If you need your “long list” to grow dynamically, it might make sense to consider a two-level structure, such as a growing array (or linked list) of pointers to fixed-size arrays of
uint16_t.Lastly, given that most modern machines are 32- or 64-bit, it may be more efficient to use either 32- or 64-bit words for this.