C++ 为什么布尔值是 1 字节而不是 1 位大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4626815/
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-28 15:58:53  来源:igfitidea点击:

Why is a boolean 1 byte and not 1 bit of size?

c++cpu-architecture

提问by Asm

In C++,

在 C++ 中,

  • Why is a boolean 1 byte and not 1 bit of size?
  • Why aren't there types like a 4-bit or 2-bit integers?
  • 为什么布尔值是 1 字节而不是 1 位大小?
  • 为什么没有像 4 位或 2 位整数这样的类型?

I'm missing out the above things when writing an emulator for a CPU

在为 CPU 编写模拟器时,我遗漏了上述内容

回答by Paul Tomblin

Because the CPU can't address anything smaller than a byte.

因为 CPU 不能寻址小于一个字节的任何东西。

回答by Nawaz

From Wikipedia:

来自维基百科

Historically, a byte was the number of bits used to encode a single character of text in a computer and it is for this reason the basic addressable elementin many computer architectures.

从历史上看,字节是用于在计算机中对文本的单个字符进行编码的位数,因此它是许多计算机体系结构中的基本可寻址元素

So byte is thebasic addressable unit, below which computer architecture cannot address. And since there doesn't (probably) exist computers which support 4-bit byte, you don't have 4-bitbooletc.

所以字节基本寻址单元,低于该计算机体系结构无法解决。而且由于不存在(可能)支持 4 位字节的计算机,因此您没有4 位bool等。

However, if you can design such an architecture which can address 4-bit as basic addressable unit, then you will have boolof size 4-bit then, on that computer only!

但是,如果您可以设计这样一种架构,可以将 4 位作为基本可寻址单元进行寻址,那么您将bool只能在该计算机上拥有4 位大小!

回答by sukru

The easiest answer is; it's because the CPU addresses memory in bytes and not in bits, and bitwise operations are very slow.

最简单的答案是;这是因为 CPU 以字节而不是位为单位寻址内存,并且按位运算非常慢。

However it's possible to use bit-size allocation in C++. There's std::vector specialization for bit vectors, and also structs taking bit sized entries.

但是,可以在 C++ 中使用位大小分配。位向量有 std::vector 特化,还有采用位大小条目的结构。

回答by Jay

Back in the old days when I had to walk to school in a raging blizzard, uphill both ways, and lunch was whatever animal we could track down in the woods behind the school and kill with our bare hands, computers had much less memory available than today. The first computer I ever used had 6K of RAM. Not 6 megabytes, not 6 gigabytes, 6 kilobytes. In that environment, it made a lot of sense to pack as many booleans into an int as you could, and so we would regularly use operations to take them out and put them in.

回到过去,我不得不在暴风雪中步行上学,双向上坡,午餐是我们可以在学校后面的树林中追踪并徒手杀死的任何动物,计算机的可用内存远少于今天。我用过的第一台计算机有 6K 的 RAM。不是 6 MB,不是 6 GB,不是 6 KB。在那种环境下,将尽可能多的布尔值打包到一个 int 中是很有意义的,因此我们会定期使用操作将它们取出并放入。

Today, when people will mock you for having only 1 GB of RAM, and the only place you could find a hard drive with less than 200 GB is at an antique shop, it's just not worth the trouble to pack bits.

今天,当人们嘲笑你只有 1 GB 的 RAM,而你唯一能找到小于 200 GB 的硬盘的地方是在古董店时,那么麻烦地收拾碎片是不值得的。

回答by Paul Sasik

You could have 1-bit bools and 4 and 2-bit ints. But that would make for a weird instruction set for no performance gain because it's an unnatural way to look at the architecture. It actually makes sense to "waste" a better part of a byte rather than trying to reclaim that unused data.

你可以有 1 位布尔值和 4 位和 2 位整数。但这会导致一个奇怪的指令集而没有性能提升,因为这是一种不自然的查看架构的方式。“浪费”一个字节的更好部分而不是试图回收那些未使用的数据实际上是有意义的。

The only app that bothers to pack several bools into a single byte, in my experience, is Sql Server.

根据我的经验,唯一需要将多个 bool 打包成一个字节的应用程序是 Sql Server。

回答by Martin York

You can use bit fields to get integers of sub size.

您可以使用位字段来获取子大小的整数。

struct X
{
    int   val:4;   // 4 bit int.
};

Though it is usually used to map structures to exact hardware expected bit patterns:

虽然它通常用于将结构映射到精确的硬件预期位模式:

struct SomThing   // 1 byte value (on a system where 8 bits is a byte
{
    int   p1:4;   // 4 bit field
    int   p2:3;   // 3 bit field
    int   p3:1;   // 1 bit
};

回答by bratao

Because a byte is the smallest addressible unit in the language.

因为字节是语言中最小的可寻址单位。

But you can make bool take 1 bit for example if you have a bunch of them eg. in a struct, like this:

但是如果你有一堆,例如,你可以让 bool 取 1 位。在一个结构中,像这样:

struct A
{
  bool a:1, b:1, c:1, d:1, e:1;
};

回答by Ryan Li

Because the generally, CPU allocates memory with 1 byte as the basic unit, although some CPU like MIPS use a 4-byte word.

因为一般CPU以1字节为基本单位分配内存,虽然有些像MIPS这样的CPU使用4字节的字。

However vectordeals boolin a special fashion, with vector<bool>one bit for each bool is allocated.

然而,以一种特殊的方式进行vector交易boolvector<bool>为每个布尔值分配一个位。

回答by franji1

Think about how you would implement this at your emulator level...

想想你将如何在你的模拟器级别实现这一点......

bool a[10] = {false};

bool &rbool = a[3];
bool *pbool = a + 3;

assert(pbool == &rbool);
rbool = true;
assert(*pbool);
*pbool = false;
assert(!rbool);

回答by Gene Bushuyev

boolcan be one byte -- the smallest addressable size of CPU, or can be bigger. It's not unusual to have boolto be the size of intfor performance purposes. If for specific purposes (say hardware simulation) you need a type with N bits, you can find a library for that (e.g. GBL library has BitSet<N>class). If you are concerned with size of bool(you probably have a big container,) then you can pack bits yourself, or use std::vector<bool>that will do it for you (be careful with the latter, as it doesn't satisfy container requirments).

bool可以是一个字节——CPU 的最小可寻址大小,也可以更大。出于性能目的而必须bool是 的大小并不罕见int。如果出于特定目的(比如硬件模拟)您需要一个 N 位类型,您可以找到一个库(例如 GBL 库有BitSet<N>类)。如果您担心bool(您可能有一个大容器)的大小,那么您可以自己打包,或者使用std::vector<bool>它来为您打包(小心后者,因为它不满足容器要求)。