C++:为什么 bool 是 8 位长?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2064550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:59:30  来源:igfitidea点击:

C++ : why bool is 8 bits long?

c++booleansize

提问by Jér?me

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 ?

在 C++ 中,我想知道为什么 bool 类型是 8 位长(在我的系统上),其中只有一位足以保存布尔值?

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 ?

我曾经认为这是出于性能原因,但是在 32 位或 64 位机器上,寄存器为 32 位或 64 位宽,性能优势是什么?

Or is it just one of these 'historical' reasons ?

或者这只是这些“历史”原因之一?

回答by jalf

Because every C++ data type must be addressable.

因为每个 C++ 数据类型都必须是可寻址的。

How would you create a pointer to a single bit? You can't. But you cancreate 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)

您将如何创建指向单个位的指针?你不能。但是您可以创建一个指向字节的指针。所以 C++ 中的布尔值通常是字节大小的。(它也可能更大。这取决于实现。主要是它必须是可寻址的,所以 C++ 数据类型不能小于一个字节)

回答by Stephen Roantree

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.

内存是字节可寻址的。如果不移位或屏蔽从内存中读取的字节,就无法寻址单个位。我想这是一个非常大的原因。

回答by jldupont

A booleantype normally follows the smallest unit of addressable memoryof the target machine (i.e. usually the 8bits byte).

boolean类型通常如下寻址存储器的最小单位的目标机器(即,通常的8位字节)的。

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.

对内存的访问总是在“块”中(多个字,这是为了硬件级别的效率,总线事务):在大多数 CPU 系统中,布尔位不能“单独”寻址。当然,一旦数据包含在寄存器中,通常会有专门的指令来独立操作位。

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.

出于这个原因,使用“位打包”技术来提高使用“布尔”基本数据类型的效率是很常见的。诸如enum(在 C 中)具有 2 次幂编码的技术就是一个很好的例子。在大多数语言中都可以找到相同的技巧。

Updated: Thanks to a excellent discussion, it was brought to my attention that sizeof(char)==1by definitionin C++. Hence, addressing of a "boolean" data type is pretty tied to the smallest unit of addressable memory (reinforces my point).

更新:由于出色的讨论,我注意到了 C++中sizeof(char)==1定义。因此,“布尔型”数据类型的寻址与可寻址内存的最小单位密切相关(加强了我的观点)。

回答by Benjamin Oakes

The answers about 8-bits being the smallest amount of memory that is addressable are correct. However, some languages canuse 1-bit for booleans, in a way. I seem to remember Pascal implementing sets as bit strings. That is, for the following set:

关于 8 位是可寻址的最小内存量的答案是正确的。但是,在某种程度上,某些语言可以将 1 位用于布尔值。我似乎记得 Pascal 将集合实现为位字符串。也就是说,对于以下集合:

{1, 2, 5, 7}

You might have this in memory:

你可能在内存中有这个:

01100101

You can, of course, do something similar in C / C++ if you want. (If you're keeping track of a bunch of booleans, it couldmake sense, but it really depends on the situation.)

当然,如果您愿意,您可以在 C/C++ 中做类似的事情。(如果您要跟踪一堆布尔值,这可能是有道理的,但这实际上取决于具体情况。)

回答by Cire

I know this is old but I thought I'd throw in my 2 cents.

我知道这很旧,但我想我会投入 2 美分。

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.

嗯,这是一个测试,看看我是否理解概念和位、字节和错误处理。一个坏人发出一点信息是多么容易。或者如果在传输过程中比特以另一种方式翻转会发生什么。

回答by EBlake

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.

一些嵌入式编译器有一个 int1 类型,用于对布尔标志进行位打包(例如,用于 Microchip MPU 的 CCS 系列 C 编译器)。设置、清除和测试这些变量使用单指令位级指令,但由于其他答案中指出的原因,编译器将不允许任何其他操作(例如获取变量的地址)。