It occurred to me that there is no built-in structure for one bit in C. There is (unsigned) char and int, which are 8 bits (one byte) and long, which are 64 + bits, and so on (uint64_t, bool .. .)
I ran into this while encoding a Huffman tree, and the encodings for certain characters did not necessarily have exactly 8 bits (e.g. 00101), so there was no efficient way to store the encodings. I had to find temporary solutions, such as strings or logical arrays, but this takes up much more memory.
But in any case, my question is more general: is there a good way to store an array of bits or some kind of user structure? I combed the network for one, but the smallest structure seems to be 8 bits (one byte). I tried things like int a : 1, but that didn't work. I read about bit fields, but they don't just achieve exactly what I want to do. I know that C ++ has already asked questions about this, and if there is a structure for one bit, but basically I want to know specifically what will be the most memory-efficient way to store the encoding, such as 00101 in C.
source
share