C ++:为什么布尔是8位长? [英] C++ : why bool is 8 bits long?
问题描述
在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 ++:为什么布尔是8位长?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!