C++ : 为什么 bool 是 8 位长?
在 C++ 中,我想知道为什么 bool 类型是 8 位长(在我的系统上),而只有一位就足以保存布尔值?
我曾经认为这是出于性能原因,但是在 32 位或 64 位机器上,寄存器为 32 或 64 位宽,性能优势是什么?
或者这只是这些“历史”原因之一?
In C++, I'm wondering why the bool type is 8 bits long (on my system), where only one bit is enough to hold the boolean value ?
I used to believe it was for performance reasons, but then on a 32 bits or 64 bits machine, where registers are 32 or 64 bits wide, what's the performance advantage ?
Or is it just one of these 'historical' reasons ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
因为每个 C++ 数据类型都必须是可寻址的。
如何创建一个指向单个位的指针?你不能。但是您可以创建一个指向字节的指针。因此,C++ 中的布尔值通常是字节大小的。 (它也可能更大。这取决于实现。最主要的是它必须是可寻址的,因此 C++ 数据类型不能小于字节)
Because every C++ data type must be addressable.
How would you create a pointer to a single bit? You can't. But you can create a pointer to a byte. So a boolean in C++ is typically byte-sized. (It may be larger as well. That's up to the implementation. The main thing is that it must be addressable, so no C++ datatype can be smaller than a byte)
内存是字节可寻址的。如果不移位或屏蔽从内存中读取的字节,则无法寻址单个位。我想这是一个非常大的原因。
Memory is byte addressable. You cannot address a single bit, without shifting or masking the byte read from memory. I would imagine this is a very large reason.
boolean
类型通常遵循目标机器的最小可寻址内存单元(即通常为 8 位字节)。对内存的访问始终以“块”形式进行(多个字,这是为了硬件级别的效率、总线事务):在大多数 CPU 系统中,布尔位无法“单独”寻址。当然,一旦数据包含在寄存器中,通常会有专门的指令来独立操作位。
因此,使用“位打包”技术来提高使用“布尔”基本数据类型的效率是很常见的。诸如具有 2 次方编码的
enum
(C 语言)之类的技术就是一个很好的例子。大多数语言中都存在同样的技巧。更新:由于一次精彩的讨论,我注意到 C++ 中定义的
sizeof(char)==1
。因此,“布尔”数据类型的寻址与可寻址内存的最小单元密切相关(强化了我的观点)。A
boolean
type normally follows the smallest unit of addressable memory of the target machine (i.e. usually the 8bits byte).Access to memory is always in "chunks" (multiple of words, this is for efficiency at the hardware level, bus transactions): a boolean bit cannot be addressed "alone" in most CPU systems. Of course, once the data is contained in a register, there are often specialized instructions to manipulate bits independently.
For this reason, it is quite common to use techniques of "bit packing" in order to increase efficiency in using "boolean" base data types. A technique such as
enum
(in C) with power of 2 coding is a good example. The same sort of trick is found in most languages.Updated: Thanks to a excellent discussion, it was brought to my attention that
sizeof(char)==1
by definition in C++. Hence, addressing of a "boolean" data type is pretty tied to the smallest unit of addressable memory (reinforces my point).关于 8 位是可寻址的最小内存量的答案是正确的。然而,某些语言可以在某种程度上使用 1 位布尔值。我似乎记得 Pascal 将集合实现为位字符串。也就是说,对于以下集合:
您的内存中可能有这样的内容:
当然,如果您愿意,您可以在 C / C++ 中执行类似的操作。 (如果您正在跟踪一堆布尔值,那么它可能有意义,但这实际上取决于具体情况。)
The answers about 8-bits being the smallest amount of memory that is addressable are correct. However, some languages can use 1-bit for booleans, in a way. I seem to remember Pascal implementing sets as bit strings. That is, for the following set:
You might have this in memory:
You can, of course, do something similar in C / C++ if you want. (If you're keeping track of a bunch of booleans, it could make sense, but it really depends on the situation.)
我知道这已经很旧了,但我想我应该投入 2 美分。
如果将布尔值或数据类型限制为一位,那么您的应用程序将面临内存损坏的风险。如何处理内存中只有一位长的错误统计信息?
我参加了一次工作面试,项目负责人对我说的其中一句话是:“当我们发送发射导弹的信号时,我们只需通过无线发送一个简单的一位开关位。发送一位的速度非常快,我们需要该信号尽可能快。”
嗯,这是一个测试,看看我是否理解概念以及位、字节和错误处理。对于一个坏人来说,发送一条比特消息是多么容易啊。或者如果在传输过程中该位被翻转到另一个方向会发生什么。
I know this is old but I thought I'd throw in my 2 cents.
If you limit your boolean or data type to one bit then your application is at risk for memory curruption. How do you handle error stats in memory that is only one bit long?
I went to a job interview and one of the statements the program lead said to me was, "When we send the signal to launch a missle we just send a simple one bit on off bit via wireless. Sending one bit is extremelly fast and we need that signal to be as fast as possible."
Well, it was a test to see if I understood the concepts and bits, bytes, and error handling. How easy would it for a bad guy to send out a one bit msg. Or what happens if during transmittion the bit gets flipped the other way.
一些嵌入式编译器具有 int1 类型,用于对布尔标志进行位打包(例如,用于 Microchip MPU 的 CCS 系列 C 编译器)。设置、清除和测试这些变量使用单指令位级指令,但编译器将不允许任何其他操作(例如获取变量的地址),原因在其他答案中指出。
Some embedded compilers have an int1 type that is used to bit-pack boolean flags (e.g. CCS series of C compilers for Microchip MPU's). Setting, clearing, and testing these variables uses single-instruction bit-level instructions, but the compiler will not permit any other operations (e.g. taking the address of the variable), for the reasons noted in other answers.
但请注意,
std::vector
允许使用位打包,即以比普通bool
更小的单位存储位。但这不是必需的。Note, however, that
std::vector<bool>
is allowed to use bit-packing, i.e. to store the bits in smaller units than an ordinarybool
. But it is not required.