将列表中的值编码为 BitArray
我有一个 N 种颜色的列表。我需要将这些值中的任何一个表示为 BitArray。如果 N = 129 到 255,显然每种颜色都应该表示为长度为 8 的 BitArray。它类似于编码数字,但是如果我知道该列表中颜色的索引,如何获得实际的 BitArray?
I have a list of N colors. I need to represent any of those values as a BitArray. If N = 129 to 255 than, 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要的是一个字节数组,而不是一个
BitArray
(当然也不是一个BitArray数组)。如果可以使用 129 到 255 之间的数字唯一定义单一颜色,则需要 8 位来表示它。BitArray
用于高效存储大量位;用它来存储 8 位是没有意义的。另一方面,您可以将字节数组(其中每个字节都是单一颜色)存储到大型
BitArray
中。但是,只有当您需要检查整个颜色列表,同时遍历多种颜色的各个位时,这才有意义。[编辑] 准确地说,正如 Henk 指出的那样,如果 N 介于 0 到 255 之间,那么您需要 8 位。如果颜色总数 <= 127,并且您使用 129 到 255 之间的数字来标记它们,那么您只需要 7 位。但我相信OP想说的是他们至少有129种颜色,最多不超过255种。
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.