为什么bool需要1个字节? [英] Why bool take 1 byte?

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

问题描述

为什么bool类型需要1个字节,因为我们知道它必须只有true或

false(1或0)表示它只需要一个位而不是整个字节

解决方案

10月31日下午4:54,Virtual_X< C.BsM .... @ gmail.comwrote:
< blockquote class =post_quotes>
为什么bool类型需要1个字节,因为我们知道它必须只有true或

false(1或0)表示它只需要一位整个字节



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


考虑:


bool foo;

bool * pfoo =& foo;

如果一个bool由一个位组成,那现在是什么?


当然,如果你愿意,你可以很容易地制作自己的位组(

节省空间)。


user923005写道:


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



我不确定它是*语言*的限制,因为它是一个

硬件限制。基本上在所有现有硬件中,一个字节是

最小可寻址内存量。


10月31日晚上8:13,user923005< dcor ... @ connx.comwrote:


10月31日下午4:54,Virtual_X< C.BsM .... @ gmail.comwrote:


为什么bool类型占用1个字节,因为我们知道它必须只有true或

false(1或0)表示它只需要一位不是整个字节



因为一个字节是该语言中最小的可寻址单元。


考虑:


bool foo;

bool * pfoo =& foo;

如果一个bool由一个位组成,现在是什么?


当然,如果你愿意,你可以很容易地制作自己的位组(

节省空间)。



对固定大小的比特阵列使用std :: bitset和

std :: vector< boolfor variable-size bitarray

然而,矢量< boolis几乎是一个被弃用的黑客;如果可以的话,

使用bitset


Why bool type take 1 byte and as we know it must have only true or
false (1 or 0) that mean that it need only one bit not the a whole byte

解决方案

On Oct 31, 4:54 pm, Virtual_X <C.BsM....@gmail.comwrote:

Why bool type take 1 byte and as we know it must have only true or
false (1 or 0) that mean that it need only one bit not the a whole byte

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

Consider:

bool foo;
bool *pfoo = &foo;
If a bool consists of a single bit, now what?

Of course, you can make your own bit sets very easily if you like (to
conserve space).


user923005 wrote:

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

I''m not sure it''s a limitation in the *language* as much as it''s a
hardware limitation. Basically in all existing hardware a byte is the
smallest addressable memory amount.


On Oct 31, 8:13 pm, user923005 <dcor...@connx.comwrote:

On Oct 31, 4:54 pm, Virtual_X <C.BsM....@gmail.comwrote:

Why bool type take 1 byte and as we know it must have only true or
false (1 or 0) that mean that it need only one bit not the a whole byte


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

Consider:

bool foo;
bool *pfoo = &foo;
If a bool consists of a single bit, now what?

Of course, you can make your own bit sets very easily if you like (to
conserve space).

use std::bitset for fixed-size bitarray and
std::vector<boolfor variable-size bitarray
however, vector<boolis pretty much a deprecated hack; if you can,
use bitset


这篇关于为什么bool需要1个字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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