数组 C++ 中每个布尔值 1 位
bool fp[81];
根据我的理解, fp 应该使用 ceil(81/8) 字节,因为它是连续的。
我说得对吗?
我怎样才能证明这一点?
bool fp[81];
From my understanding fp should use ceil(81/8) bytes because it is in succession.
Am I correct?
How can I prove this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不,缓冲区的大小是实现定义的,根据标准的引用:
因此,您可以预期的大小是 81 * X,其中 X 是 bool 的大小,这是实现定义的。
No, the sizeof your buffer is implementation defined, as per this quote from the Standard:
Therefore the size you can expect is 81 * X, where X is the size of bool, which is implementation defined.
不,它的
81*sizeof(bool)
很可能是 81 字节no, its
81*sizeof(bool)
which is most likely 81 bytes您可以使用 sizeof 找出任何对象或类型使用的存储空间:
You can find out the storage used by any object or type with sizeof:
你可以使用
sizeof(fp)
检查它的大小,在我的例子中给出 81you can check its size using
sizeof(fp)
which in my case gives 81不,每个布尔值通常单独存储(通常,具体取决于您的计算机,8 位)。占用的内存至少为 81 字节。
No, each bool is usually stored separately (usually, depending on your computer, 8-bits). The memory occupied would be a minimum of 81 bytes.
如果您想确保每个位都被视为一个位,而不是使用一个字节表示整个值,请使用位集:
您必须了解这是处理器及其指令的非常低级的内存约束,因为它们被设计为支持位字,而不是位。
不过,为此目的添加一些指令本来是一件好事,因为 bitset 所做的只是位移位......
我真的需要学习 x86 汇编。
Use a bitset if you want to be sure each bit will be considered as a bit instead of using a byte for a whole value:
You have to understand this is a very low level memory constraint of processors and their instructions, as they are designed to support words of bits, not bits.
It would have been a good thing that some instructions would have been added for this purpose though, since the thing bitset does is just bit shifting...
I really need to learn x86 assembly.
不,
bool
是 8 位。使用vector
(专门的位打包向量)或bitset
。No, a
bool
is 8 bits. Usevector<bool>
(a specialized bit-packed vector) orbitset
.