由于在大多数C++环境中不存在这样的小元素类型,因此使用特殊引用来访问各个元素,这些引用模仿了
bool
元素。
那么,这个位引用是如何工作的呢?
我所能想到的唯一方法是使用char
的静态数组,但每个实例都需要在数组中存储其索引。由于每个引用实例至少具有size_t
大小,这将破坏位集的紧凑性。此外,调整大小可能会很慢,而位操作应该很快。
由于在大多数C++环境中不存在这样的小元素类型,因此使用特殊引用来访问各个元素,这些引用模仿了
bool
元素。
那么,这个位引用是如何工作的呢?
我所能想到的唯一方法是使用char
的静态数组,但每个实例都需要在数组中存储其索引。由于每个引用实例至少具有size_t
大小,这将破坏位集的紧凑性。此外,调整大小可能会很慢,而位操作应该很快。
/**
* This encapsulates the concept of a single bit. An instance of this
* class is a proxy for an actual bit; this way the individual bit
* operations are done as faster word-size bitwise instructions.
*
* Most users will never need to use this class directly; conversions
* to and from bool are automatic and should be transparent. Overloaded
* operators help to preserve the illusion.
*
* (On a typical system, this <em>bit %reference</em> is 64
* times the size of an actual bit. Ha.)
*/
我还没有查看STL源代码,但我期望Bitset引用包含指向实际bitset的指针和大小为size_t的位数。只有在尝试获取bitset元素的引用时才会创建引用。
正常使用bitset时,不太可能广泛使用引用(如果有的话),因此不应该有太多性能问题。而且,它在概念上类似于char
类型。一个char通常是8位,但要存储对char的“引用”需要一个指针,因此通常是32或64位。
我从未查看过参考实现,但显然它必须通过引用知道它所指的位集,并且负责更改的位的索引。然后,它可以使用其余的位集接口来进行所需的更改。这可能非常高效。请注意,位集无法调整大小。
我不太确定你在问什么,但我可以告诉你一种访问字节中单个位的方法,这也许就是bitset所做的事情。请注意,以下代码不是我自己写的,而是微软规范(!)。
创建一个如下的结构体:
struct Byte
{
bool bit1:1;
bool bit2:1;
bool bit3:1;
bool bit4:1;
bool bit5:1;
bool bit6:1;
bool bit7:1;
bool bit8:1;
}
这段代码中的“:1”部分是位域。http://msdn.microsoft.com/en-us/library/ewwyfdbe(v=vs.80).aspx它们定义了变量所需占用的位数,因此在这个结构体中,有8个布尔值,每个占用1位。因此,“Byte”结构体的大小为1字节。
现在,如果您有一个字节的数据,比如一个字符,您可以将这个数据存储在一个Byte对象中,如下所示:
char a = 'a';
Byte oneByte;
oneByte = *(Byte*)(&a); // Get the address of a (a pointer, basically), cast this
// char* pointer to a Byte*,
// then use the reference operator to store the data that
// this points to in the variable oneByte.
现在,您可以通过访问oneByte的bool成员变量来访问(和更改)单个位。为了再次将更改后的数据存储在char中,您可以执行以下操作:
char b;
b = *(char*)(&oneByte); // Basically, this is the reverse of what you do to
// store the char in a Byte.
我会尝试找到这种技术的来源,并给予应有的荣誉。
此外,我不完全确定这个答案是否对您有用。我理解您的问题是“如何处理对内部个别位的访问?”
std::bitset
和std::bitset::reference
。 - Matteo Italia