Encoding value from the list as a BitArray
I have a list of N colors. I need to represent any of those values as a BitArray. If N = 129 to 255 tha开发者_运维技巧n, obviously, each color should be represented as a BitArray with Length 8. It is similar to encoding numbers, but how can I get an actual BitArray, if I know an index of the color from that list?
What you need is an array of bytes, not a BitArray
(and certainly not an array of BitArrays). If a single color can be uniquely defined using a number between 129 and 255, then you need 8 bits to represent it.
BitArray
is used to efficiently store a large array of bits; using it to store 8 bits wouldn't make sense.
On the other hand, you can store an array of bytes (where each byte is a single color) into a large BitArray
. But that would only make sense if you need to examine the list of colors as a whole, traversing through individual bits of multiple colors at once.
[Edit] To be precise, as Henk pointed out, if N is between 0 and 255, then you need 8 bits. If total number of colors is <= 127, and you mark them using numbers from 129 to 255, then you only need 7 bits. But I believe what OP wanted to say they will have at least 129 colors, and no more than 255.
精彩评论