C++:为什么 bool 是 8 位长? [英] C++ : why bool is 8 bits long?

查看:76
本文介绍了C++:为什么 bool 是 8 位长?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 ?

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

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 ?

推荐答案

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

Because every C++ data type must be addressable.

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

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)

这篇关于C++:为什么 bool 是 8 位长?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆