C ++中位集的大小是多少 [英] What is the size of bitset in C++

查看:118
本文介绍了C ++中位集的大小是多少的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道位集实际上是如何分配内存的.我从某个博客中了解到,它占用了内存一点点的空间.但是,当我运行以下代码时:

I want to know how bitset actually allocates memory. I read from some blog that it takes up memory in bits. However when i run the following code:

   bitset<3> bits = 001;
   cout<<sizeof(bits);

我得到的输出为4.其背后的解释是什么?

还有没有一种方法可以在C ++中按位分配空间?

I get the output as 4. What is the explanation behind it?

Also is there a method to allocate space in bits in C++?

推荐答案

您可以将sizeof(bitset<N>)近似为:

  1. 如果内部表示形式为4 * ((N + 31) / 32)
  2. ,则内部表示形式为32位(例如在32位系统上为无符号).
  3. 如果内部表示形式为8 * ((N + 63) / 64),则内部表示形式为64位(例如在64位系统上为unsigned long)
  1. If internal representation is 32bit (like unsigned on 32bit systems) as 4 * ((N + 31) / 32)
  2. If internal representation is 64bit (like unsigned long on 64bit systems) as 8 * ((N + 63) / 64)

第一个似乎是对的:4 * ((3 + 31) / 32)4

It seems that the first is true: 4 * ((3 + 31) / 32) is 4

这篇关于C ++中位集的大小是多少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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